Categories
Code

Custom Metaboxes for WordPress: Front Page show_on filter

Shows the metabox only if a static front page is set and you’re editing it.


/*
Make sure your metabox is set thus:
'pages' => array( 'page' ), // Post
'show_on' => array( 'key' => 'front-page', 'value' => '' ),
*/

/**
* Include metabox on front page
* @author Ed Townend
* @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 ed_metabox_include_front_page( $display, $meta_box ) {
if ( 'front-page' !== $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 ID of page set as front page, 0 if there isn't one
$front_page = get_option('page_on_front');

if ( $post_id == $front_page ) {
//there is a front page set and we're on it!
return $display;
}

}
add_filter( 'cmb_show_on', 'ed_metabox_include_front_page', 10, 2 );