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).
Wow, this is pretty cool. Thanks for sharing with us. This will certainly help me in my WordPress projects
This is nearly exactly what I’m looking for. Except rather than having a custom field meta box, I only want a blank meta box with a title and some text, much like a simple information meta box that I can rag and drop around the page edit control panel.
If I ask really, really nicely would you be able to show me how that would be coded for a specific page.
Thanks heaps. Cheers, Pete
Pete, you inspired me to write another tutorial, take a look at How to Create a Custom WordPress Meta Box. I’m sure you’ll be able to adapt it to your needs.
Thanks for that… I’ll digest it and see how I go… thanks again 🙂
Question:
Considering WP 3.0 will have custom post types, would the following code make a meta box appear only under a defined post type?
Let’s say I have a custom post type named “realestate”.
Would the following make my custom meta box appear only under Real Estate (and not in Posts):
if ($_POST[‘post_type’] == ‘realestate’)
{
…
}
Thanks in advance 🙂
Cosmin, I’ve update the post above, custom post types should work just fine …
Exactly what I was looking for today. Even posted on the WP forums but have hence added your solution there.
http://wordpress.org/support/topic/408909?replies=2
(And hey, what’s up with the nifty addthis widget? How do you get it to start sticking after scrolling past the header?)
How about adding a meta box specific to a group of pages under a page parent. I’m struggling with adding the following conditional into your function. I thought declaring global $post would do it but it won’t show.
@Ross, do use the WPAlchemy_MetaBox class for creating your meta boxes. It has a filtering mechanism (doesn’t support parent_post ID, but I think i will add it) … meanwhile you could wrap the class initialization in an if and that would work.
As for your question, see if the following works:
I think you can’t use
is_page()
while doing things in the admin UI … hence the code above uses GET and POST to try to get the post ID.Works! Thanks for the quick reply Dimas.
WPAlchemy class looks dope. Depending how many of these darn meta boxes I need will most likely be useful. Is there a plugin on the roadmap?
If I’m using the WPAlchemy_MetaBox class for creating my meta boxes, what code would I place in functions.php to have the meta boxes only display on a custom post type e.g. events?
And I guess for calling the values I’d just follow “using the meta box values in your template” for the custom template I set up for events?
Justin, see the setup section of the documentation, specifically the
types
parameter.You would do something like:
You can also refer to the theme template section for using the values.
You sir are the man. You are the man. Who’s the man? Your the man.
Thanks so much Dimas I’ve subscribed to feed, twitter, and facebook and will be posting about how awesome you are over on GeekEstate Blog – PR5!
Justin, I’m glade I could help and most of all I hope that you found the WPAlchemy_MetaBox class useful … I always do appreciate backlinks 😉
Hey what should I use if I’m going to make visible a meta box in a specific page
Thank You
Sha, take a look at WPAlchemy and use the “include_post_id” option.
Hi Dimas.
I really appreciate your tutorials, i have one question for ya, is it possible to get this to update automatically when switching template?
Thx !
Johan, can you elaborate a little more? Typically you are adding code in your “functions.php” file which usually travels with your theme, which means that when you do switch themes your functions.php changes are lost. Using wordpress’ plugin functionality is useful for creating custom code that needs to live through out theme changes.
Hi again
Yeah now when i read it again its a pretty confusing question.
Im doing this things in the function.php so when im saying update automatically i mean when your choosing page template design.
So when choosing lets say the startpage template design i want a set of custom write panels to appear, right now i need to update it with either publish or save to draft before these feild appear.
im thinking that this might be doable with some jquery and ajax?
I would do this with custom post types but the thing is that custom post types rewrites the url structure.
I hope this was better explained 🙂
Hi Dimas
Your examples above shows how to add metabox for a specific post id.
Is it possible to make it specific to that post id AND any post that is a subpage of that post id too ?
scratch that, I just saw your answer to Ross up there.
anyway, Thank you for this wonderful script !
Paul, if you haven’t checked out wpalchemy, I encourage you to give it a shot, it abstracts a lot of the above and makes things much easier.
This is great! However, the only downside to is that the user must save or update the page/post before the metabox appears. Is there any way to have this done via AJAX? Maybe set the metabox to auto-appear, add a filter to hide it (via default_hidden_meta_boxes hook), then uncheck it to have it auto appear? Just thinking out loud. Thoughts?
Good idea to use the hook. I think it just hides it with css. Then you can use js to show it. Take a look at this, might be helpful too.
I think if I use the hook, which hides it, but not via CSS. The CSS that hides it is .hide-if-js {display:none;}
What the JS/jQ needs to do is select/unselect the checkmark in the screen options form (in .metabox-prefs).
I’ve started a fiddle: http://jsfiddle.net/SAbxU/12/
The JS does a good job of hiding/unhiding the metabox. However, I need it to check/uncheck the box at the top too.
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:
I then test to see if a page is the page I would like to display the metabox, which works more a less fine:
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.
This will help all of you
http://www.paulund.co.uk/display-post-meta-box-specific-page-templates