Created
September 8, 2011 18:50
-
-
Save billerickson/1204283 to your computer and use it in GitHub Desktop.
Limit metabox to specific page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Changes to metabox array, new field 'show_on' | |
$meta_boxes[] = array( | |
'id' => 'blog-options', | |
'title' => 'Blog Options', | |
'pages' => array('page'), | |
'show_on' => array( 'key' => 'id', 'value' => array( 12 ) ), | |
'context' => 'normal', | |
'priority' => 'low', | |
'show_names' => true, | |
'fields' => array( | |
array( | |
'name' => 'Query Arguments', | |
'desc' => '', | |
'id' => 'query_args', | |
'type' => 'text' | |
), | |
), | |
); | |
// Changes to init.php, new add() function | |
function add() { | |
$this->_meta_box['context'] = empty($this->_meta_box['context']) ? 'normal' : $this->_meta_box['context']; | |
$this->_meta_box['priority'] = empty($this->_meta_box['priority']) ? 'high' : $this->_meta_box['priority']; | |
foreach ( $this->_meta_box['pages'] as $page ) { | |
if( !isset( $this->_meta_box['show_on'] ) ) { | |
add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']) ; | |
} else { | |
if ( 'id' == $this->_meta_box['show_on']['key'] ) { | |
// If we're showing it based on ID, get the current ID | |
if( isset( $_GET['post'] ) ) $post_id = $_GET['post']; | |
elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID']; | |
// If current page id is in the included array, display the metabox | |
if ( isset( $post_id) && in_array( $post_id, $this->_meta_box['show_on']['value'] ) ) | |
add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']) ; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment