parse_args( 'select', array( 'class' => 'cmb2_select', 'name' => $field_type_object->_name(), 'id' => $field_type_object->_id(), ) ); $attrs['data-close-on-select'] = 'true'; // Support for multiple if( $field->args['multiple'] ) { $attrs['name'] = $attrs['name'] . '[]'; $attrs['multiple'] = 'true'; $attrs['data-close-on-select'] = 'false'; } // Parse options $options = ''; foreach ( $field->options() as $value => $option ) { if( is_array( $option ) ) { // Options group $options .= ''; foreach ( $option as $key => $label ) { $selected = ( is_array( $escaped_value ) && in_array( $key, $escaped_value ) ) || $escaped_value === $key; $options .= sprintf( '', $key, $selected ? 'selected="selected"' : '', $label ); } $options .= ''; } else { $selected = ( is_array( $escaped_value ) && in_array( $value, $escaped_value ) ) || $escaped_value === $value; //Single option $options .= sprintf( '', $value, $selected ? 'selected="selected"' : '', $option ); } } echo sprintf( '%s%s', $field_type_object->concat_attrs( $attrs ), $options, $field_type_object->_desc( true ) ); } add_action( 'cmb2_render_advanced_select', 'cmb2_render_advanced_select_field_type', 10, 5 ); /** * Sanitize the selected value. */ function cmb2_sanitize_advanced_select_callback( $override_value, $value ) { // Sanitize multiple value if ( is_array( $value ) ) { foreach ( $value as $key => $saved_value ) { $value[$key] = sanitize_text_field( $saved_value ); } return $value; } return sanitize_text_field( $value ); } add_filter( 'cmb2_sanitize_advanced_select', 'cmb2_sanitize_advanced_select_callback', 10, 2 ); endif;