Last active
August 29, 2015 14:23
-
-
Save ediamin/7ba1307c62692295b89b to your computer and use it in GitHub Desktop.
How to change/update CMB2 field in child theme
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 | |
/***************************************************** | |
==================PARENT THEME========================= | |
******************************************************/ | |
/** | |
* Suppose this is the function that contains the cmb2 metaboxes | |
* in your parent theme. We want to change the name of the first | |
* metabox from "Banner Text" to "Banner Content" and then change it's | |
* type from "text" to "textarea" | |
*/ | |
function my_cmb2_metaboxes( array $meta_boxes ) { | |
// page metaboxes | |
$meta_boxes['page_metaboxes'] = array( | |
'id' => 'page_metaboxes', // YOU NEED THIS ID TO HOOK. SEE BOTTOM COMMENT | |
'title' => 'Meta Options', | |
'object_types' => array( 'page' ), | |
'context' => 'side', | |
'priority' => 'default', | |
'show_names' => true, | |
'fields' => array( | |
array( | |
'name' => 'Banner Text', // this will be changed in child theme | |
'id' => '_page_banner_text', | |
'type' => 'text', // this will be changed in child theme | |
), | |
array( | |
'name' => 'Banner Image', | |
'id' => '_page_banner_image', | |
'type' => 'file', | |
'options' => array( | |
'url' => false, | |
), | |
), | |
), | |
); | |
return $meta_boxes; | |
} | |
add_filter( 'cmb2_meta_boxes', 'my_cmb2_metaboxes' ); | |
/***************************************************** | |
==================CHILD THEME========================= | |
******************************************************/ | |
/** | |
* This hook in child theme will change the cmb2 cofigured in parent theme | |
*/ | |
add_action( 'cmb2_init_page_metaboxes', 'edited_cmb2_config', 10, 1 ); | |
function edited_cmb2_config( $cmb2 ) { | |
$cmb2->update_field_property( '_page_banner_text', 'name', 'Banner Content' ); | |
$cmb2->update_field_property( '_page_banner_text', 'type', 'textarea' ); | |
} | |
/** | |
* The tag for action hook depends on the metabox id. Since my id was "page_metaboxes" | |
* that is why I use cmb2_init_page_metaboxes. Take a look at the end of the __constructor | |
* of main CMB2 class in includes/CMB2.php in the cmb2 folder | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment