
Make your WordPress custom meta boxes page specific. The home page is a great example of this: often the main page will contain several unique elements which would benefit from using custom meta boxes, but how do you limit the meta box to the home page only?
I recently worked on a WordPress site which had a custom homepage that used a few Custom Fields. I decided to setup a custom meta box to make the UI a little bit more pleasent for the editors.
Making a WordPress MetaBox Page or Post Type Specific
Because this particular meta box was specific to one page, I only wanted it to appear when editing that page. Setting this up is pretty straight forward, additionally you can also limit the meta box by template type and post types (This will prove really useful with the introduction of custom post types in WordPress 3.0):
add_action('admin_init','my_meta_init');
function my_meta_init()
{
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// checks for post/page ID
if ($post_id == '84')
{
add_meta_box('my_all_meta_1', 'My Custom Meta Box 1', 'my_meta_setup_1', 'page', 'normal', 'high');
}
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
// check for a template type
if ($template_file == 'home.php')
{
add_meta_box('my_meta_2', 'My Custom Meta Box 2', 'my_meta_setup_2', 'page', 'normal', 'high');
}
// add a meta box for custom page types
foreach (array('events','page') as $type)
{
add_meta_box('my_meta_3', 'My Custom Meta Box 3', 'my_meta_setup_3', $type, 'normal', 'high');
}
add_action('save_post','my_meta_save');
}
The meta box only gets defined and displayed when you are editing the specific post/page (in this case post ID 84, all other pages will not need to use the meta box, so it doesn’t need to be visible for those pages).
The $_GET['post'] is used when the you first click “Edit” to edit the post/page and the $_POST['post_ID'] is needed when the post/page is updated/published (to detect the current post/page when the form data is submitted).

Hello,
This is great, thanks you for the code.
I’m using similar code. I’ve got a metabox array with all its settings, like so:
$meta_boxes[] = array( 'id' => 'taglines', 'title' => 'Home Page Taglines', 'pages_limit' => array('page_slugs' => array('home')), 'fields' => array( array( 'name' => 'Taglines', 'id' => $prefix . 'taglines', 'type' => 'wysiwyg', 'desc' => 'Specify taglines as an unordered list.' ) ) );I then test to see if a page is the page I would like to display the metabox, which works more a less fine:
global $post; // add metaboxes to the specific pages only if(!empty($this->_meta_box['pages_limit'])){ //page types if(!empty($this->_meta_box['pages_limit']['page_slugs'])){ foreach( $this->_meta_box['pages_limit']['page_slugs'] as $page_slug ){ if( $post->post_name === $page_slug ){ //add metaboxes add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $post->post_type, $this->_meta_box['context'], $this->_meta_box['priority']); } //end if $page_slug_str } //end foreach as $page_slug } //end if !empty($page_slugs) } //end if !empty($pages_limit)However, the only buggy thing is that is displays an empty (no content) HTML structure for all the pages where metabox shouldn’t be displayed at all. I can’t understand why the empty HTML structure is even inserted on the pages where metabox shouldn’t appear.
I would really appreciate if someone should explain.
Many thanks,
Dasha
When you say “empty HTML structure”, where is this appearing? On a frontend template page or in the wp-admin somewhere? Are you sure you meta box code is the cause?
Hey dude cheers for this, has saved me a lot of searching, appreciate it, i’m going to write an article on this once i get my site finished, i will be sure to link back to this article.
Cheers,
Lee.
great piece of code here…really helpful..thank you for sharing!!!
Hi,
I use your code, it works well in the front end.. but all the extra meta box that i put in a specific page always show in in edit/new post.
Can you help me with this
Thank you very much for this – will make my WordPress custom builds so much better.
Fantastic post. Thank you.
Thank you so much for this post it has saved me many hours in learning how to make meta boxes appear on posts and pages.
Hi!
how to only for
`if (is_front_page () )`
I’m with alrobeler, I’d love to limit this to just the front page, in order to give the home page a set of changeable variables.
Brilliant and to the point ,perfect A+++
Nice code, but I feel like this feature should be included in WordPress by default. It feels a bit hackish to do it this way, but it works.
I want to add_meta_box for specific custom post type. The custom type post is called Quotes and I want that the meta_box to be appeared only in Quotes’ posts. Can anyone suggest me how to fix it? Thanks.