Skip to content

Custom admin boxes

Patrick edited this page Jun 11, 2015 · 10 revisions

If the admin box filters are not enough and you want complete control of the admin interface, you can roll your own admin box.

Here's some code to get you started:

<?php

add_action( 'p2p_init', 'init_my_connection_type', 100 );
add_action( 'add_meta_boxes', 'register_my_meta_box' );

function init_my_connection_type() {
	p2p_register_connection_type( array(
		'name' => 'posts_to_pages',
		'from' => 'post',
		'to' => 'page',
		'admin_box' => false
	) );
}

function register_my_meta_box() {
	add_meta_box( 'my-box', 'My Connected Pages', 'render_my_meta_box', 'post' );
}

function render_my_meta_box( $post ) {
	echo 'This is my box.';

	$connected = p2p_type( 'posts_to_pages' )->get_connected( $post );
?>

<ul>
<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
}

Note that you have to handle connection saving yourself. See Creating connections programmatically.