Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created September 8, 2011 18:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/1204283 to your computer and use it in GitHub Desktop.
Save billerickson/1204283 to your computer and use it in GitHub Desktop.
Limit metabox to specific page
<?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