Skip to content

Instantly share code, notes, and snippets.

@codigoconjuan
Created June 30, 2015 21:00
Show Gist options
  • Save codigoconjuan/addeff6330c8612e5cdf to your computer and use it in GitHub Desktop.
Save codigoconjuan/addeff6330c8612e5cdf to your computer and use it in GitHub Desktop.
add_action( 'cmb2_init', 'yourprefix_register_about_page_metabox' );
/**
* Hook in and add a metabox that only appears on the 'About' page
*/
function yourprefix_register_about_page_metabox() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_yourprefix_about_';
/**
* Metabox to be displayed on a single page ID
*/
$cmb_about_page = new_cmb2_box( array(
'id' => 'contact-information',
'title' => 'Contact Information',
'object_types' => array( 'page' ), // post type
'cmb2_show_on' => array( 'key' => 'page-template', 'value' => 'page-nosidebar.php' ),
'context' => 'normal', // 'normal', 'advanced', or 'side'
'priority' => 'high', // 'high', 'core', 'default' or 'low'
'show_names' => true, // Show field names on the left
) );
$cmb_about_page->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'text',
'type' => 'text',
) );
}
/**
* Metabox for Page Template
* @author Kenneth White
* @link https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-show_on-filters
*
* @param bool $display
* @param array $meta_box
* @return bool display metabox
*/
function be_metabox_show_on_template( $display, $meta_box ) {
if ( ! isset( $meta_box['show_on']['key'], $meta_box['show_on']['value'] ) ) {
return $display;
}
if ( 'template' !== $meta_box['show_on']['key'] ) {
return $display;
}
$post_id = 0;
// 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 ( ! $post_id ) {
return false;
}
$template_name = get_page_template_slug( $post_id );
$template_name = ! empty( $template_name ) ? substr( $template_name, 0, -4 ) : '';
// See if there's a match
return in_array( $template_name, (array) $meta_box['show_on']['value'] );
}
add_filter( 'cmb2_show_on', 'be_metabox_show_on_template', 10, 2 );
add_action( 'cmb2_init', 'yourprefix_register_about_page_metabox' );
/**
* Hook in and add a metabox that only appears on the 'About' page
*/
function yourprefix_register_about_page_metabox() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_yourprefix_about_';
/**
* Metabox to be displayed on a single page ID
*/
$cmb_about_page = new_cmb2_box( array(
'id' => 'contact-information',
'title' => 'Contact Information',
'object_types' => array( 'page' ), // post type
'cmb2_show_on' => array( 'key' => 'page-template', 'value' => 'page-nosidebar.php' ),
'context' => 'normal', // 'normal', 'advanced', or 'side'
'priority' => 'high', // 'high', 'core', 'default' or 'low'
'show_names' => true, // Show field names on the left
) );
$cmb_about_page->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'text',
'type' => 'text',
) );
}
/**
* Metabox for Page Template
* @author Kenneth White
* @link https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-show_on-filters
*
* @param bool $display
* @param array $meta_box
* @return bool display metabox
*/
function be_metabox_show_on_template( $display, $meta_box ) {
if ( ! isset( $meta_box['show_on']['key'], $meta_box['show_on']['value'] ) ) {
return $display;
}
if ( 'template' !== $meta_box['show_on']['key'] ) {
return $display;
}
$post_id = 0;
// 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 ( ! $post_id ) {
return false;
}
$template_name = get_page_template_slug( $post_id );
$template_name = ! empty( $template_name ) ? substr( $template_name, 0, -4 ) : '';
// See if there's a match
return in_array( $template_name, (array) $meta_box['show_on']['value'] );
}
add_filter( 'cmb2_show_on', 'be_metabox_show_on_template', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment