WordPress.org

Ready to get started?Download WordPress

Forums

Help, trying to use a multi-select box in my widget options panel? (4 posts)

  1. datdesignguy
    Member
    Posted 2 years ago #

    Hi guys,

    I'm designing a widget where I would like to display the categories available for a custom post type, and then allow the end user to select multiple categories that they would like to include in the query for this widget.

    The widget options form displays just fine (before a user has save any settings, but after the instance's options have been saved, the multi-select widget goes wonky and only displays the "Use All Groups" default option.

    Can someone please have a look at my current form() and update() functions from my widget? I've pasted the entire widget code here:

    http://pastebin.com/kcAzMxGv

    After saving the options for a new widget, the options in the select box all throw an error saying that the $use_groups variable in my form() function isn't an array.

    (I originally posted this in the main Plugins & Hacks forum, then remembered I should've posted it here)
    Can anyone help me figure out what I'm doing wrong here?

  2. datdesignguy
    Member
    Posted 2 years ago #

    I managed to kill the error that was coming up by changing the <select> box name attribute to <?php $this->get_field_name( 'use_groups'); ?>[] however, only the last element in the array of selected options is getting saved to the database. So that no matter how many options I select, the last one is the only one being saved.

    Does anyone have any ideas here?

    Thanks!

  3. fusionstream
    Member
    Posted 2 years ago #

    <select multiple='multiple' name='THEGETFIELDNAMECODE[]' style='width: 99%;'>

    replace THEGETFIELDNAMECODE with $this->get_field_name('name');

    You use the get field name thing so that wordpress can format the name so that it's engine can properly receive your data.

    Just remember that web browsers don't create arrays for you. So you need to trick your php backend with a name like somename[].

  4. fusionstream
    Member
    Posted 2 years ago #

    * $this->get_field_name('use_groups')

Topic Closed

This topic has been closed to new replies.

About this Topic