views:

307

answers:

1

I'mt trying to create a plugin which adds a custom page in the admin panel. I have a checkbox with name "deposit_sandbox" if check true it should update the option in the database to true, if not selected it should update the option in the database to false.

how do I check if a checkbox was selected and then update the database with update_option() function?

It works if I leave the action blank and then use $_POST, but I would need to do this for each item in my form. if there is a workaround please let me know :)

+1  A: 

If your options have the same name as the checkboxes, you can write the following:

$options_names = array("deposit_sandbox", "name2", "name3");
foreach($options_names as $option_name){
    $checked = isset($_POST[$option_name]);
    update_option($option_name, $checked);
}

I don't remember if update_option accepts boolean values (like $checked in my example). If it doesn't, change the $checked line for $checked = isset($_POST["deposit_sandbox"]) ? 1 : 0;.

I'm supposing you create default values to all your options when you activate the plugin.

GmonC
yes I do. I'll give that a try
krike