Can I limit this meta box to a particular page?

Inside your add_meta_boxex hook callback function, you will have an add_meta_box() call. Wrap that call in a conditional, using data from the $post global (I’m fairly certain it is available in edit.php). For example, you could use either the Page ID or slug.

Page ID:

global $post;
if ( '123' == $post->ID ) {
    // Page has ID of 123, add meta box
    add_meta_box( $args );
}

Page slug:

global $post;
$slug = basename( get_permalink( $post->ID ) );
if ( 'contact' == $slug ) {
    // Page has ID of 123, add meta box
    add_meta_box( $args );
}

Note: you can also target the edit.php page, using the $pagenow global, e.g.:

global $pagenow, $page;
if ( 'edit.php' = $pagenow && '123' == $post->ID ) {
    add_meta_box( $args );
}

However, it might be more efficient just to target the appropriate add_meta_boxes hook for your callback. For example, your add_action() call probably looks like this:

add_action( 'add_meta_boxes', 'callback_function_name' );

But, you could instead use the add_meta_boxes_{post_type} hook, to target Pages specifically:

add_action( 'add_meta_boxes_page', 'callback_function_name' );

That way, the callback only gets called in the Page post-type context.

Leave a Comment

techhipbettruvabetnorabahisbahis forumutaraftarium24eduedueduedueduedueduedusedusedu