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

Leave a Reply