Can I limit this meta box to a particular page?

I am using a version of this (http://www.deluxeblogtips.com/meta-box-script-for-wordpress/) meta box script but I want to be able to limit which edit screen a meta box shows.

For example, if I only want a meta box to show in the “contact” page edit screen, is that possible?

$meta_boxes[] = array(
'id' => 'project-box-1',                            // meta box id, unique per meta box
'title' => 'Project Box 1',         // meta box title
'pages' => array('page'),   // post types, accept custom post types as well, default is array('post'); optional
'context' => 'normal',                      // where the meta box appear: normal (default), advanced, side; optional
'priority' => 'high',                       // order of meta box: high (default), low; optional

Related posts

Leave a Reply

2 comments

  1. 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.

  2. See I tend to think a little different than developers. I would make a custom template and associate this with just that template. This way it exists and if you want to add it to another page you use the new custom template.

    I don’t have the code in front of me right now.