Last active
September 8, 2022 16:19
-
-
Save damiencarbery/5531a3c959f2613b4e778d9dc17a13b0 to your computer and use it in GitHub Desktop.
CMB2 only on front page - Use a custom 'show_on_cb' function to only show the CMB2 metabox when editing the front page. https://www.damiencarbery.com/2020/02/use-cmb2-on-front-page/
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 | |
function dcwd_show_cmb2_on_front_page( $cmb ) { | |
// Get ID of page set as front page, 0 if there isn't one | |
$front_page = get_option( 'page_on_front' ); | |
// There is a front page set - are we editing it? | |
return $cmb->object_id() == $front_page; | |
} |
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 | |
/* | |
Plugin Name: CMB2 only on front page | |
Plugin URI: https://www.damiencarbery.com/2020/02/use-cmb2-on-front-page/ | |
Description: Use a custom 'show_on_cb' function to only show the CMB2 metabox when editing the front page. | |
Author: Damien Carbery | |
Version: 0.2 | |
*/ | |
add_action( 'cmb2_admin_init', 'dcwd_home_page_listings' ); | |
function dcwd_home_page_listings() { | |
$prefix = '_dcwd_'; | |
$cmb = new_cmb2_box( array( | |
'id' => $prefix . 'home_page_listings', | |
'title' => 'Column 2 Settings', | |
'object_types' => array( 'page' ), | |
'context' => 'after_editor', | |
'show_names' => true, | |
'show_on_cb' => 'dcwd_show_cmb2_on_front_page', | |
) ); | |
$cmb->add_field( array( | |
'name' => 'Column 2 title', | |
'desc' => 'Enter the text to be shown above the posts in column 2.', | |
'id' => $prefix . 'col2title', | |
'type' => 'text', | |
) ); | |
$cmb->add_field( array( | |
'name' => 'Number of posts to show', | |
'desc' => 'Numbers only', | |
'id' => $prefix . 'col2count', | |
'type' => 'text', | |
// Ensure that only numbers are entered. | |
'attributes' => array( | |
'type' => 'number', | |
'pattern' => '\d*', | |
), | |
) ); | |
} | |
function dcwd_show_cmb2_on_front_page( $cmb ) { | |
// Get ID of page set as front page, 0 if there isn't one | |
$front_page = get_option( 'page_on_front' ); | |
// There is a front page set - are we editing it? | |
return $cmb->object_id() == $front_page; | |
} | |
// Verify that CMB2 plugin is active. | |
add_action( 'admin_notices', 'verify_cmb2_active' ); | |
function verify_cmb2_active() { | |
if ( ! defined( 'CMB2_LOADED' ) ) { | |
$plugin_data = get_plugin_data( __FILE__ ); | |
$plugin_name = $plugin_data['Name']; | |
?> | |
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div> | |
<?php | |
//error_log( 'CMB2 is not active.' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment