0

I have this code in my wordpress plugin but get error Notice: Undefined index: type . how to solve this? I searched a lot, but I didn't find a solution, or maybe I didn't understand it correctly because I'm not very professional.

public function cwlp_field_callback( $field ) {
    $value = get_option( $field['id'] );
    $placeholder = '';
    if ( isset($field['placeholder']) ) {
        $placeholder = $field['placeholder'];
    }
    switch ( $field['type'] ) {
                    case 'select':
                        case 'multiselect':
                            if( ! empty ( $field['options'] ) && is_array( $field['options'] ) ) {
                                $attr = '';
                                $options = '';
                                foreach( $field['options'] as $key => $label ) {
                                    $options.= sprintf('<option value="%s" %s>%s</option>',
                                        $key,
                                        selected($value, $key, false),
                                        $label
                                    );
                                }
                                if( $field['type'] === 'multiselect' ){
                                    $attr = ' multiple="multiple" ';
                                }
                                printf( '<select name="%1$s" id="%1$s" %2$s>%3$s</select>',
                                    $field['id'],
                                    $attr,
                                    $options
                                );
                            }
                            break;

        default:
            printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />',
                $field['id'],
                $field['type'],
                $placeholder,
                $value
            );
    }
}
masoud nkh
  • 72
  • 9
  • It means the `$field` doesn't have `type` key. So you have to check whether the `type` key exists or not and if it is there then you can use it. – hNczy Nov 09 '22 at 09:18

0 Answers0