1

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
1
  • Just add var_dump($_REQUEST); in some function that gets loaded on admin_head or similar. This way you can see what the exact request is. Btw: Care about your open Qs. 25% accept rate is too low for most ppl to give real answers.
    – kaiser
    Sep 23 '11 at 13:02
3

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.

1

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.

4
  • It depends on the nature of the content being added via meta box. But if it requires user input of data (text, etc.), rather than merely a checkbox or select, then those data have to be input somewhere, either as Post Custom Meta, or else as a Theme Option. Sep 23 '11 at 15:55
  • @Chris - Do you still have this script that you mentioned??? Your idea is exactly what Ive been after for some time now with no avail. Targeting the use of a specific template makes so much more sense to me as well.
    – Mr.Brown
    May 1 '12 at 3:06
  • @Mr.Brown - I don't have control of the site where I had something like that set-up. I believe we used advanced custom fields for the input. I believe we used the approach suggested on advancedcustomfields.com/support/discussion/34/… May 1 '12 at 11:21
  • @ChipBennett - sorry I didn't respond sooner. I understand that, but selecting individual pages by ID or global all posts or pages - doesn't seem like a good idea. I much prefer a customized set-up with either page templates or custom posts for any customized content types. May 1 '12 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.