Skip to content

Instantly share code, notes, and snippets.

@tatemz
Created November 8, 2015 19:48
Show Gist options
  • Save tatemz/709dd7fb5607cd5e40b2 to your computer and use it in GitHub Desktop.
Save tatemz/709dd7fb5607cd5e40b2 to your computer and use it in GitHub Desktop.
Add "Other" option to allow for custom radio input with CMB2
<?php
if ( file_exists( dirname( __FILE__ ) . '/cmb2/init.php' ) ) {
require_once dirname( __FILE__ ) . '/cmb2/init.php';
} elseif ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
require_once dirname( __FILE__ ) . '/CMB2/init.php';
}
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
/**
* Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
*/
function yourprefix_register_demo_metabox() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_yourprefix_demo_';
/**
* Sample metabox to demonstrate each field type included
*/
$cmb_demo = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page', ), // Post type
// 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
// 'context' => 'normal',
// 'priority' => 'high',
// 'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // true to keep the metabox closed by default
) );
$cmb_demo->add_field( array(
'name' => __( 'Test Radio With Custom Input', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'radio',
'type' => 'radio_other',
'show_option_none' => 'None',
'show_option_other' => 'Custom Radio',
'options' => array(
'option1' => __( 'Option One', 'cmb2' ),
'option2' => __( 'Option Two', 'cmb2' ),
'option3' => __( 'Option Three', 'cmb2' ),
),
) );
}
<?php
add_action( 'cmb2_render_radio_other', 'yourprefix_radio_other', 10, 5 );
function radio_other( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
$other = '';
if ( $field->args && array_key_exists( 'show_option_other', (array) $field->args ) ) {
$a = $field_type_object->parse_args( $args, 'list_input_other', array(
'type' => 'radio',
'class' => 'cmb2-option',
'name' => $field_type_object->_name(),
'id' => $field_type_object->_id( 'other' ),
'value' => '',
'label' => $field->args['show_option_other'],
'onclick' => sprintf( "document.getElementById('%s').focus()", $field_type_object->_id( 'othertext' ) ),
) );
$b = $field_type_object->parse_args( $args, 'list_input_other_text', array(
'type' => 'text',
'class' => 'cmb2-option',
'name' => '',
'id' => $field_type_object->_id( 'othertext' ),
'value' => '',
'label' => $field->args['show_option_other'],
'onclick' => sprintf( "document.getElementById('%s').checked = true", $field_type_object->_id( 'other' ) ),
'onkeyup' => sprintf( "document.getElementById('%s').value = document.getElementById('%s').value", $field_type_object->_id( 'other' ), $field_type_object->_id( 'othertext' ) ),
) );
$input = sprintf( '<input%s/>', $field_type_object->concat_attrs( $b, array( 'label' ) ) );
$other = sprintf( "\t" . '<li><input%s/> <label for="%s">%s</label> %s</li>' . "\n", $field_type_object->concat_attrs( $a, array( 'label' ) ), $a['id'], $a['label'], $input );
}
$a = $field_type_object->parse_args( $field, 'radio', array(
'class' => 'cmb2-radio-list cmb2-list',
'options' => $field_type_object->concat_items( array( 'label' => 'test', 'method' => 'list_input' ) ),
'desc' => $field_type_object->_desc( true ),
) );
printf( '<ul class="%s">%s%s</ul>%s', $a['class'], $a['options'], $other, $a['desc'] );
}
add_filter( 'cmb2_list_input_other_text_attributes', 'yourprefix_other_text_attributes', 10, 4 );
function other_text_attributes( $args, $defaults, $field, $field_type_object ) {
$value = $field->escaped_value()
? $field->escaped_value()
: $field->args( 'default' );
if ( $value && ! array_key_exists( $value, $field->options() ) ) {
$args['value'] = $value;
}
return $args;
}
add_filter( 'cmb2_list_input_other_attributes', 'yourprefix_other_radio_attributes', 10, 4 );
function other_radio_attributes( $args, $defaults, $field, $field_type_object ) {
$value = $field->escaped_value()
? $field->escaped_value()
: $field->args( 'default' );
if ( $value && ! array_key_exists( $value, $field->options() ) ) {
$args['checked'] = true;
$args['value'] = $value;
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment