• I’m working on a WordPress widget right now that has dynamic checkboxes that the user can add. The checkboxes are used to create different categories for the widget. The problem I’m having is saving the state of these checkboxes (whether they’re checked or not). Current I have it setup so the label is saved in the WordPress database, but when I try to save the state it just doesn’t work.

    Here is the full widget class:
    http://pastebin.com/81zKFuih

    It should be saving the state in the update method, but for some reason it’s not. I tried placing the for loop both before and after the call to parseAndCombineCategories() but it didn’t make a difference. If I hard code the option (i.e. $instance[‘categories’][0][‘selected’] = 1) it works, but if I try to set instance equal to new_instance like I do in the for loop nothing is saved in the selected node of the array. Something I actually noticed too is if there was already a 0 saved in the selected node and it runs through that loop 0 is no longer there, it’s replaced with nothing.

Viewing 4 replies - 1 through 4 (of 4 total)
  • looking at your code, you aren’t passing/setting a value for the checkboxes

    i.e. <input type="checkbox" name="whatever" value="true">
    so whether it’s checked or not, it has a value of “” the way you have it setup..

    if you add a value, then when it’s checked, the value returned is whatever you define, if it’s not checked, then the value returned is “”

    Thread Starter kschat

    (@kschat)

    Whoa, you’re right, whoops. I added value=”1″ for testing purposes right now and I’m still getting the same results I explained above.

    Edit: Forgot to end my comment section at the top of my file, here’s an updated version. http://pastebin.com/2Bws78rr

    did you try echoing $_POST to see what’s being sent?

    i.e. echo "<PRE>".print_r($_POST,true)."</PRE>";

    Thread Starter kschat

    (@kschat)

    Okay, after much tinkering and ‘hacking’ I found out why it wasn’t saving and how to fix it. It wasn’t saving because the values were saving to the post variable with a key equal to the value that is stored in my categories variable label node (worded it oddly, sorry ha). Anyway I did a ‘quick fix’ to get it to work so I have something to show to my boss at my meeting today. The fix was setting the name parameter to this:

    name="<?php echo "widget-image-uploader-id[2][categories][" . $i . "][selected]"; ?>"

    My long term fix is to create a categories class and make a list of that. Thank you very much for your help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Checkbox state won't save in widget’ is closed to new replies.