Last active
December 21, 2015 01:28
-
-
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.
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 | |
/** | |
* 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