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

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

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

  3. idris

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

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

  5. Joke de Winter

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

  6. Jon Spencer

    Fantastic post. Thank you.

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

  8. alrobeler

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

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

  10. Samrat Saha

    Brilliant and to the point ,perfect A+++

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

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

Leave a Reply