Skip to content

Instantly share code, notes, and snippets.

@ramseyp
Last active December 21, 2015 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramseyp/6227383 to your computer and use it in GitHub Desktop.
Save ramseyp/6227383 to your computer and use it in GitHub Desktop.
If you use this custom meta box code: https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress then this gist may be useful. It allows you to use the page slug as a "show-on" filter.
<?php
/**
* Use the page slug to filter the 'show-on' of a meta box
* @author Pat Ramsey
* @link https://gist.github.com/ramseyp/6227383
*
* @param bool $display
* @param array $meta_box
* @return bool display metabox
*/
add_filter( 'cmb_show_on', 'add_for_page_slug', 10, 2 );
// Add for Page Slug
function add_for_page_slug( $display, $meta_box ) {
if ( 'page-slug' !== $meta_box['show_on']['key'] )
return $display;
// Get the current ID
if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
if( !( isset( $post_id ) || is_page() ) ) return false;
// Get page slug
$cur_page = get_post( $post_id );
$page_slug = $cur_page->post_name;
// If value isn't an array, turn it into one
$meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];
// See if there's a match
if( in_array( $page_slug, $meta_box['show_on']['value'] ) )
return true;
else
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment