-
-
Save sc0ttkclark/e959ca54d47a02b48c84 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Example: Add discography example | |
*/ | |
if ( ! function_exists( 'cmb2_example_plugin_post_discography' ) ) { | |
function cmb2_example_plugin_post_discography() { | |
$prefix = '_cmb2_example_post_discography_'; | |
/** | |
* Initiate the metabox | |
*/ | |
$cmb = new_cmb2_box( array( | |
'id' => $prefix, | |
'title' => __( 'Discography', 'cmb2' ), | |
'object_types' => array( 'post', ), // 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' => __( 'Album year', 'cmb2' ), | |
// 'desc' => __( 'field description (optional)', 'cmb2' ), | |
'id' => $prefix . 'year', | |
'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, | |
) );*/ | |
$group_field_id = $cmb->add_field( array( | |
'name' => 'Albums', | |
'id' => $prefix . 'albums', | |
'type' => 'group', | |
'description' => __( 'List of albums', 'cmb' ), | |
'options' => array( | |
'group_title' => __( 'Album {#}', 'cmb' ), // since version 1.1.4, {#} gets replaced by row number | |
'add_button' => __( 'Add Another Album', 'cmb' ), | |
'remove_button' => __( 'Remove Album', 'cmb' ), | |
'sortable' => true, // beta | |
), | |
) ); | |
$cmb->add_group_field( $group_field_id, array( | |
'name' => 'Album Title', | |
'id' => 'title', | |
'type' => 'text', | |
) ); | |
$cmb->add_group_field( $group_field_id, array( | |
'name' => 'Album Year', | |
'id' => 'year', | |
'type' => 'text_small', | |
) ); | |
$tracks_group_field_id = $cmb->add_group_field( $group_field_id, array( | |
'name' => 'Tracks', | |
'id' => 'tracks', | |
'type' => 'group', | |
'description' => __( 'List of tracks', 'cmb' ), | |
'options' => array( | |
'group_title' => __( 'Track {#}', 'cmb' ), // since version 1.1.4, {#} gets replaced by row number | |
'add_button' => __( 'Add Another Track', 'cmb' ), | |
'remove_button' => __( 'Remove Track', 'cmb' ), | |
'sortable' => true, // beta | |
), | |
) ); | |
$tracks_group_field_id = $tracks_group_field_id[1]; // Get field ID | |
$cmb->add_group_field( $tracks_group_field_id, array( | |
'name' => 'Track Title', | |
'id' => 'title', | |
'type' => 'text', | |
) ); | |
$cmb->add_group_field( $tracks_group_field_id, array( | |
'name' => 'Track Length', | |
'id' => 'length', | |
'type' => 'text_small', | |
) ); | |
$cmb->add_group_field( $tracks_group_field_id, array( | |
'name' => 'Composer', | |
'id' => 'title', | |
'type' => 'text', | |
'repeatable' => true, | |
) ); | |
$buy_now_group_field_id = $cmb->add_group_field( $tracks_group_field_id, array( | |
'name' => 'Buy Now Links', | |
'id' => 'buy_now_links', | |
'type' => 'group', | |
'description' => __( 'List of stores', 'cmb' ), | |
'options' => array( | |
'group_title' => __( 'Store {#}', 'cmb' ), // since version 1.1.4, {#} gets replaced by row number | |
'add_button' => __( 'Add Another Store', 'cmb' ), | |
'remove_button' => __( 'Remove Store', 'cmb' ), | |
'sortable' => true, // beta | |
), | |
) ); | |
$buy_now_group_field_id = $buy_now_group_field_id[1]; // Get field ID | |
$cmb->add_group_field( $buy_now_group_field_id, array( | |
'name' => 'Store Name', | |
'id' => 'length', | |
'type' => 'text', | |
) ); | |
$cmb->add_group_field( $buy_now_group_field_id, array( | |
'name' => 'Link', | |
'id' => 'length', | |
'type' => 'text', | |
) ); | |
$singles_group_field_id = $cmb->add_field( array( | |
'name' => 'Singles', | |
'id' => $prefix . 'singles', | |
'type' => 'group', | |
'description' => __( 'List of singles', 'cmb' ), | |
'options' => array( | |
'group_title' => __( 'Single {#}', 'cmb' ), // since version 1.1.4, {#} gets replaced by row number | |
'add_button' => __( 'Add Another Single', 'cmb' ), | |
'remove_button' => __( 'Remove Single', 'cmb' ), | |
'sortable' => true, // beta | |
), | |
) ); | |
$cmb->add_group_field( $singles_group_field_id, array( | |
'name' => 'Single Title', | |
'id' => 'title', | |
'type' => 'text', | |
) ); | |
$cmb->add_group_field( $singles_group_field_id, array( | |
'name' => 'Single Length', | |
'id' => 'length', | |
'type' => 'text_small', | |
) ); | |
$cmb->add_group_field( $singles_group_field_id, array( | |
'name' => 'Composer', | |
'id' => 'title', | |
'type' => 'text', | |
'repeatable' => true, | |
) ); | |
$buy_now_single_group_field_id = $cmb->add_group_field( $singles_group_field_id, array( | |
'name' => 'Buy Now Links', | |
'id' => 'buy_now_links', | |
'type' => 'group', | |
'description' => __( 'List of stores', 'cmb' ), | |
'options' => array( | |
'group_title' => __( 'Store {#}', 'cmb' ), // since version 1.1.4, {#} gets replaced by row number | |
'add_button' => __( 'Add Another Store', 'cmb' ), | |
'remove_button' => __( 'Remove Store', 'cmb' ), | |
'sortable' => true, // beta | |
), | |
) ); | |
$buy_now_single_group_field_id = $buy_now_single_group_field_id[1]; // Get field ID | |
$cmb->add_group_field( $buy_now_single_group_field_id, array( | |
'name' => 'Store Name', | |
'id' => 'length', | |
'type' => 'text', | |
) ); | |
$cmb->add_group_field( $buy_now_single_group_field_id, array( | |
'name' => 'Link', | |
'id' => 'length', | |
'type' => 'text', | |
) ); | |
} | |
} | |
add_filter( 'cmb2_init', 'cmb2_example_plugin_post_discography' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment