I have a plugin with multiple options, on frontend I'm using an angular app which will show all these options, so I need to save it in the DB as a JSON.
I have around 10 options, and I want all 10 to be saved in only 1 row in wp_options
in a JSON format, currently update_option("fee", $_POST[$key]);
saves only the last option.
if(array_key_exists('submit_settings', $_POST))
{
foreach($global_settings->globals[0] as $key => $value)
{
update_option("fee", $_POST[$key]);
}
?>
<div id="setting-error-settings-update" class="updated settings_error notice is-dismissible"> <stron> Settings have been saved. </strong> </div>
<?php
}
foreach($global_settings->globals[0] as $key => $value)
{
echo '<label for='.$key.'>' . $key . '</label>';
echo '<input name='.$key.' value='.$value.' /> <br />';
}
From my understanding, each time the for loop is executed you are overwriting the option fee
, so everytime only the last option will be saved. What you must do is to json_enocde
the whole array and save it as a single value in the DB. When you fetch it you can json_decode
to get all the values.
Something like this
$fees = [ 'one' =>'opt1', 'two' => 'opt2', 'three' => 'opt3'];
update_option("fee", json_encode($options));
// In get option
$get_options = json_decode(get_option("fee"));