Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Created March 29, 2024 15:58
Show Gist options
  • Save jtsternberg/d6ab21ca3a7de1a77d1adfefa0b9445f to your computer and use it in GitHub Desktop.
Save jtsternberg/d6ab21ca3a7de1a77d1adfefa0b9445f to your computer and use it in GitHub Desktop.
Testing CMB2 options page w/ datetime field
<?php
function yourprefix_register_main_options_metabox() {
/**
* Registers main options page menu item and form.
*/
$args = array(
'id' => 'yourprefix_main_options_page',
'title' => 'Main Options',
'object_types' => array( 'options-page' ),
'option_key' => 'yourprefix_main_options',
'tab_group' => 'yourprefix_main_options',
'tab_title' => 'Main',
);
// 'tab_group' property is supported in > 2.4.0.
if ( version_compare( CMB2_VERSION, '2.4.0' ) ) {
$args['display_cb'] = 'yourprefix_options_display_with_tabs';
}
$main_options = new_cmb2_box( $args );
// do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this );
add_action( 'cmb2_after_options-page_form_yourprefix_main_options_page', function( $object_id ) {
$val = get_option( $object_id );
print( '<style>html{background:#f1f1f1;}</style><xmp style="background:#fff;margin: 2em auto;padding: 1em 2em;">'. __FUNCTION__ . ':' . __LINE__ .') '. print_r( get_defined_vars(), true ) .'</xmp>' );
} );
/**
* Options fields ids only need
* to be unique within this box.
* Prefix is not needed.
*/
$main_options->add_field( array(
'name' => 'Site Background Color',
'desc' => 'field description (optional)',
'id' => 'bg_color',
'type' => 'colorpicker',
'default' => '#ffffff',
) );
$main_options->add_field( array(
'name' => __( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'text_datetime_timestamp_timezone',
'type' => 'text_datetime_timestamp_timezone',
'time_format' => 'H:i',
'date_format' => 'Y-m-d',
// 'repeatable' => true,
'before_field' => function ( $args, $field ) {
$option = get_option( 'yourprefix_main_options' );
$value = $option[ $field->id() ] ?? '';
$_utc_value = $option[ $field->id() . '_utc' ] ?? '';
echo '<xmp>$value: '. print_r( $value, true ) .'</xmp>';
echo '<xmp>$_utc_value: '. print_r( $_utc_value, true ) .'</xmp>';
},
'column' => true,
) );
$main_options->add_field( array(
'name' => 'text datetime timestamp timezone',
'desc' => 'serialized DateTime object',
'id' => 'text_datetime_timestamp_timezone2',
'type' => 'text_datetime_timestamp_timezone',
// 'repeatable' => true,
'before_field' => function ( $args, $field ) {
$option = get_option( 'yourprefix_main_options' );
$value = $option[ $field->id() ] ?? '';
$_utc_value = $option[ $field->id() . '_utc' ] ?? '';
echo '<xmp>$value: '. print_r( $value, true ) .'</xmp>';
echo '<xmp>$_utc_value: '. print_r( $_utc_value, true ) .'</xmp>';
},
'column' => true,
) );
}
add_action( 'cmb2_admin_init', 'yourprefix_register_main_options_metabox' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment