-
Notifications
You must be signed in to change notification settings - Fork 260
Admin box filters
scribu edited this page Mar 22, 2013
·
40 revisions
Table of contents:
Example: Ordering pages alphabetically
Given the 'posts_to_pages' connection type, say you want to display the candidate pages alphabetically instead of chronologically when creating connections.
You can do this with a little bit of PHP:
<?php
function order_pages_by_title( $args, $ctype, $post_id ) {
if ( 'posts_to_pages' == $ctype->name && 'to' == $ctype->get_direction() ) {
$args['orderby'] = 'title';
$args['order'] = 'asc';
}
return $args;
}
add_filter( 'p2p_connectable_args', 'order_pages_by_title', 10, 3 );
Note that, since we check the direction, only pages will be affected by this change.
Example: Create published pages
By default, when you create a page directly from the P2P admin box, it's status will be 'draft'. If you want it to be published immediately, you can do something like this:
<?php
function p2p_published_by_default( $args, $ctype ) {
if ( 'posts_to_pages' == $ctype->name && 'to' == $ctype->get_direction() ) {
$args['post_status'] = 'publish';
}
return $args;
}
add_filter( 'p2p_new_post_args', 'p2p_published_by_default', 10, 2 );
Here are the filters which you can use for altering the admin boxes behaviour:
-
'p2p_connectable_args'
- called before querying for posts that the user can connect to the current post. -
'p2p_connected_args'
- called before querying for posts that are already connected to the current post. -
'p2p_new_post_args'
- called before creating a post via the "New" tab.
All of the above filters pass the follwing arguments to the callbacks:
- array
$args
- the current arguments that are being filtered. - object
$ctype
- the current P2P_Connection_Type instance. - mixed
$post_id
- the current post we're dealing with
<?php
function restrict_p2p_box_display( $show, $ctype, $post ) {
if ( 'posts_to_pages' == $ctype->name && 'to' == $ctype->get_direction() ) {
return ( 'YOUR-TEMPLATE.php' == $post->page_template );
}
return $show;
}
add_filter( 'p2p_admin_box_show', 'restrict_p2p_box_display', 10, 3 );