PHP (Wordpress) - Select multiple - show the "selected" state does not work

It seems I cannot get multiple selections to display the selected = "selected" attribute when saving or updating.

Everything works (the db tab is fine, the output is OK), but I can’t get the selected values ​​that will be displayed after the update.

I am trying to use the selected () function from the wp core.

Code (it is inside the widget class and serves as a fairly simple widget):

 <!-- List Custom Fields Hide Underscore (hide=no) --> <p> <?php $keys_no = $this->k99_cfsw_get_all_cf_array('no');?> <?php if ( $keys_no ) { ?> <label for="<?php echo $this->get_field_id('cfl2'); ?>"><?php _e('Select your custom field','k99_gelosa_domain'); ?></label> <select multiple="multiple" class="chzn-select" style="width:150px;height:29px;" size="5" id="<?php echo $this->get_field_id('cfl2'); ?>" name="<?php echo $this->get_field_name('cfl2'); ?>[]" value="<?php $instance['cfl2'] ?>"> <option value="<?php isset($instance['cfl2']) ? _e($instance['cfl2']) : _e('#NONE#'); ?>"><?php isset($instance['cfl2']) ? _e('multiple') /*_e($instance['cfl2'])*/ : _e('#NONE#'); ?></option> <!--<option value="#NONE#"><?php //echo $cfl; ?></option>--> <!--<option value="------">-------</option>--> <?php foreach ( $keys_no as $key ) { echo "\n<option value='" .esc_attr($key) . "'". selected( $instance['cfl2'],esc_attr($key) ,FALSE).">". esc_html($key) . "</option>"; } ?> </select> <?php } ?> </p> 

as a side note and a bonus question - I also use the selected JS - it also works fine (except that it does not display the selected elements ..), but also has a small error that it does not trigger when the widget is closed - only after saving in the open state ...

+4
source share
1 answer

no-mind, found it, answer:

  selected(true, in_array($key, $instance['cfl2']), false) 
+9
source

All Articles