39 responses to “How to Limit the Display of a WordPress Meta Box”

  1. Meditation Ebooks

    Wow, this is pretty cool. Thanks for sharing with us. This will certainly help me in my WordPress projects

  2. Pete

    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

  3. Pete

    Thanks for that… I’ll digest it and see how I go… thanks again 🙂

  4. Cosmin

    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 🙂

  5. Ross

    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?)

  6. Ross

    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.

    global $post;
    if (is_page() && $post->post_parent == '3488')
    {
    	add_meta_box('my_meta', 'My Custom Meta Box', 'my_meta_setup', 'page', 'normal', 'high');
    }
    
  7. Ross

    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?

  8. Justin Bartlett

    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?

  9. Justin Bartlett

    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!

  10. Sha

    Hey what should I use if I’m going to make visible a meta box in a specific page
    Thank You

  11. Johan

    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 !

  12. Johan

    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 🙂

  13. paul

    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 ?

  14. paul

    scratch that, I just saw your answer to Ross up there.

    anyway, Thank you for this wonderful script !

  15. Travis Smith

    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?

  16. Travis Smith

    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.

  17. Dasha

    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

  18. lee

    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.

  19. idris

    great piece of code here…really helpful..thank you for sharing!!!

  20. endrian

    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

  21. Joke de Winter

    Thank you very much for this – will make my WordPress custom builds so much better.

  22. Jon Spencer

    Fantastic post. Thank you.

  23. nav

    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.

  24. alrobeler

    Hi!
    how to only for
    `if (is_front_page () )`

  25. marty

    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.

  26. Samrat Saha

    Brilliant and to the point ,perfect A+++

  27. Pieter

    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.

  28. Bob

    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.

  29. Bipin Sapkota