Custom field that adds option groups (optgroup) to select field
- This topic has 1 reply, 2 voices, and was last updated 6 years, 2 months ago by
Anh Tran.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
February 4, 2019 at 8:00 PM #13203
pluginoven
ParticipantSince the walk function was removed, the class described in thread:
https://support.metabox.io/topic/option-group-support-for-select-select-advanced-field-types/
no longer works. Here is the updated class for a new selectomatic field type that adds optgroup functionality:if ( class_exists( 'RWMB_Field' ) ) { class RWMB_Selectomatic_Field extends RWMB_Select_Field { public static function html( $meta, $field ) { $options = self::transform_options( $field['options'] ); $attributes = self::call( 'get_attributes', $field, $meta ); $attributes['data-selected'] = $meta; $walker = new RWMB_Walker_Selectomatic( $field, $meta ); $output = sprintf( '<select %s>', self::render_attributes( $attributes ) ); if ( ! $field['multiple'] && $field['placeholder'] ) { $output .= '<option value="">' . esc_html( $field['placeholder'] ) . '</option>'; } $output .= $walker->walk( $options, $field['flatten'] ? -1 : 0 ); $output .= '</select>'; $output .= self::get_select_all_html( $field ); return $output; } } class RWMB_Walker_Selectomatic extends RWMB_Walker_Select { public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) { $meta = $this->meta; if($depth){ $output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $object->value ), selected( in_array( $object->value, $meta ), true, false ), esc_html( RWMB_Field::filter( 'choice_label', $object->label, $this->field, $object ) ) ); } else{ $output .= sprintf( '<optgroup label="%s">', esc_html( RWMB_Field::filter( 'choice_label', $object->label, $this->field, $object ) ) ); } } public function rwmb_end_html_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) { if(!$depth){ $output .= '</optgroup>'; } } } }
February 11, 2019 at 4:15 PM #13256Anh Tran
KeymasterThanks a lot for your help. I've added it to the code snippet library.
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.