1

Before WordPress 4.4 recent update, I was able to create an array instance in a widget from a multiple select menu this way:

<?php $cats = get_categories(); ?>  
<select multiple="multiple" name="<?php echo $this->get_field_name('cats][') ?>">
    <?php foreach ( $cats as $cat ) { ?>
        <option value="<?php echo $cat->term_id ?>"><?php echo $cat->name; ?></option>
    <?php } ?>
</select>

The type of $instance['cats'] generated from this code was an array before that update, now it's a string.

What has changed and how can I solve it?

=======

UPDATE: Complete code of a widget:

###################################################################

function widet_sample() {
    register_widget('widet_sample');
}
add_action('widgets_init', 'widet_sample');

class widet_sample extends WP_Widget {

    function widet_sample() {
        parent::__construct(false, 'SAMPLE');
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['cats'] = $new_instance['cats'];
        return $instance;
    }

    function form($instance) {
?>
<p>
    Categories IDs: <?php print_r($instance['cats']); ?>
</p>
<p>
    <?php $cats = get_categories(); ?>  
    <select class="widefat" multiple="multiple" name="<?php echo $this->get_field_name('cats][') ?>">
        <?php foreach ( $cats as $cat ) { ?>
            <option value="<?php echo $cat->term_id ?>"><?php echo $cat->name; ?></option>
        <?php } ?>
    </select>
</p>    
<?php
    }
}

###################################################################

Illustration

4
  • 1) Does your field name really have square brackets paired back to back like that? 2) You haven't set $instance['cats'] at all. Please post the code in enough context that the problem can be duplicated.
    – s_ha_dum
    Commented Dec 9, 2015 at 22:44
  • 1) Yes. This is how it worked before the update, and this is what I used to output an array. 2) Updated my question with a complete reproducible code. Commented Dec 9, 2015 at 22:54
  • Much better, thanks. That should help a lot. But may I ask why you have ][ in your field name? That looks a bit hackish and it looks like the kind of thing that could (should) get sanitized away.
    – s_ha_dum
    Commented Dec 9, 2015 at 23:19
  • Yes, it's a hack. I couldn't think of any other way to output an array value from a multiple select menu in a widget other than that. It was made as a match to using <select multiple="multiple" name="options_group[field_name][]"> in my theme options file. Commented Dec 9, 2015 at 23:27

1 Answer 1

0

Problem solved! Still don't know what has changed in the 4.4 update though.

Prior to 4.4, you can use the square brackets reverted to generate an array instance, like that:

<select multiple="multiple" name="<?php echo $this->get_field_name('cats][') ?>">
//$instance['cats'] = array();

Starting from 4.4, you can use square brackets in regular case, like that:

<select multiple="multiple" name="<?php echo $this->get_field_name('cats[]') ?>">
//$instance['cats'] = array();

Or, use the generic and best approach:

<select multiple="multiple" name="<?php echo $this->get_field_name('cats') ?>[]">
//$instance['cats'] = array();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.