Skip to content

Instantly share code, notes, and snippets.

@fredguth
Created June 20, 2014 15:51
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 fredguth/57e23770c3d563890154 to your computer and use it in GitHub Desktop.
Save fredguth/57e23770c3d563890154 to your computer and use it in GitHub Desktop.
Display Metabox on Post Format
/**
* Show metabox on post format
* @author @fredguth
* @link https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Adding-your-own-show_on-filters
*
* @param bool $display
* @param array $meta_box
* @return bool display metabox
*/
function fg_metabox_show_on_post_format( $display, $meta_box ) {
if ( 'post-format' !== $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'];
}
//return false early if there is no ID
if( !isset( $post_id ) ) return false;
//Get format of the post
$post_format = get_post_format( $post_id );
// 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'];
return in_array( $post_format, $meta_box['show_on']['value'] );
}
add_filter( 'cmb_show_on', 'fg_metabox_show_on_post_format', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment