Created
April 11, 2017 17:20
-
-
Save braginteractive/9e11d569f5d0978c0f26588051e1a496 to your computer and use it in GitHub Desktop.
CMB2 example meta boxes
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
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' ); | |
/** | |
* Define the metabox and field configurations. | |
*/ | |
function cmb2_sample_metaboxes() { | |
// Start with an underscore to hide fields from custom fields list | |
$prefix = '_yourprefix_'; | |
/** | |
* Initiate the metabox | |
*/ | |
$cmb = new_cmb2_box( array( | |
'id' => 'test_metabox', | |
'title' => __( 'Test Metabox', 'cmb2' ), | |
'object_types' => array( 'page', ), // Post type | |
'context' => 'normal', | |
'priority' => 'high', | |
'show_names' => true, // Show field names on the left | |
// 'cmb_styles' => false, // false to disable the CMB stylesheet | |
// 'closed' => true, // Keep the metabox closed by default | |
) ); | |
// Regular text field | |
$cmb->add_field( array( | |
'name' => __( 'Test Text', 'cmb2' ), | |
'desc' => __( 'field description (optional)', 'cmb2' ), | |
'id' => $prefix . 'text', | |
'type' => 'text', | |
'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value | |
// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter | |
// 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter | |
// 'on_front' => false, // Optionally designate a field to wp-admin only | |
// 'repeatable' => true, | |
) ); | |
// URL text field | |
$cmb->add_field( array( | |
'name' => __( 'Website URL', 'cmb2' ), | |
'desc' => __( 'field description (optional)', 'cmb2' ), | |
'id' => $prefix . 'url', | |
'type' => 'text_url', | |
// 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols | |
// 'repeatable' => true, | |
) ); | |
// Email text field | |
$cmb->add_field( array( | |
'name' => __( 'Test Text Email', 'cmb2' ), | |
'desc' => __( 'field description (optional)', 'cmb2' ), | |
'id' => $prefix . 'email', | |
'type' => 'text_email', | |
// 'repeatable' => true, | |
) ); | |
// Add other metaboxes as needed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment