Skip to content

Instantly share code, notes, and snippets.

@timothyjensen
Created April 12, 2017 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothyjensen/ae66306c64b549f8dcf41d5ae00444a7 to your computer and use it in GitHub Desktop.
Save timothyjensen/ae66306c64b549f8dcf41d5ae00444a7 to your computer and use it in GitHub Desktop.
CMB2 on Genesis Archive Settings.
<?php
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
/**
* Creating a metabox for a Genesis CPT Archive Settings page, where the CPT is 'webinars'.
*/
function yourprefix_register_demo_metabox() {
/**
* Sample metabox to demonstrate each field type included
*/
$cmb_demo = new_cmb2_box( array(
'id' => 'genesis-cpt-archive-settings-webinars-metabox',
'name' => __( 'Test Text', 'cmb2' ),
'hookup' => false,
'cmb_styles' => true,
'show_on' => array(
'key' => 'options-page',
'value' => 'genesis-cpt-archive-webinars',
),
) );
$cmb_demo->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'genesis-cpt-archive-settings-webinars[test_text]',
'type' => 'text',
) );
}
/**
* Hook in to the Genesis CPT Archive Settings form for 'webinars'.
*/
add_action( 'webinars_page_genesis-cpt-archive-webinars_settings_page_boxes', function() {
$cmb = cmb2_get_metabox( 'genesis-cpt-archive-settings-webinars-metabox', 0, 'options-page' );
$cmb->show_form();
} );
@timothyjensen
Copy link
Author

timothyjensen commented Apr 12, 2017

Justin, the last thing to iron out is populating the field after the data is saved to the options table. I can retrieve the option using

$options = get_option( 'genesis-cpt-archive-settings-webinars');
$test_text = $options['test_text'];

But the value is not populated in the CMB2 field; it remains empty after the page refreshes. See https://cl.ly/1J202h101f3x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment