I have data in the wp_options
table currently stored as a multi-dimensional array (profile_element_order
):
a:12:{s:17:"img_base64_enable";s:1:"1";s:25:"moulding_combination_page";s:0:"";s:24:"moulding_collection_page";s:0:"";s:25:"idea_gallery_thumb_height";s:3:"200";s:24:"idea_gallery_thumb_width";s:3:"200";s:23:"collection_thumb_height";s:3:"200";s:22:"collection_thumb_width";s:3:"200";s:20:"profile_item_columns";s:1:"4";s:17:"idea_item_columns";s:1:"2";s:24:"collections_item_columns";s:1:"2";s:25:"combinations_item_columns";s:1:"4";s:21:"profile_element_order";a:5:{i:0;s:8:"Option 1";i:1;s:8:"Option 2";i:2;s:8:"Option 3";i:3;s:8:"Option 4";i:4;s:8:"Option 5";}}
What I’m trying to accomplish is update the profile_element_order
option (within those options). Here’s how everything looks so far:
function psort_save_order() {
global $mouldings_options;
$list = $mouldings_options['profile_element_order'];
$new_order = $_POST['list_items'];
$new_list = array();
// update order
foreach($new_order as $v) {
if(isset($list[$v])) {
$new_list[$v] = $list[$v];
}
}
// save the new order
update_option('profile_element_order', $new_list);
die();
}
add_action('wp_ajax_psort_update_order', 'psort_save_order');
The data is correctly posting to the DB table (as I can see some of my failed attempts as new option entries, like mouldings_settings->profile_element_order
) — I’m just having a hard time figuring out the update_option()
syntax for just that specific option. I’ve tried things like (bearing in mind `mouldings_settings is the actual option name):
mouldings_settings['profile_element_order']
$mouldings_options['profile_element_order']
profile_element_order
but no dice at the moment. Any pointers would be greatly appreciated! Thanks!
Update
This is what I have now — ajax action saves fine, but when I save the plugin options, it duplicates the options in the database and throws up the same error as before:
a:17:{s:17:"img_base64_enable";s:1:"1";s:25:"moulding_combination_page";s:0:"";s:24:"moulding_collection_page";s:0:"";s:25:"idea_gallery_thumb_height";s:3:"200";s:24:"idea_gallery_thumb_width";s:3:"200";s:23:"collection_thumb_height";s:3:"200";s:22:"collection_thumb_width";s:3:"200";s:20:"profile_item_columns";s:1:"4";s:17:"idea_item_columns";s:1:"2";s:24:"collections_item_columns";s:1:"2";s:25:"combinations_item_columns";s:1:"4";s:21:"profile_element_order";a:5:{i:4;s:8:"Option 5";i:0;s:8:"Option 1";i:1;s:8:"Option 2";i:3;s:8:"Option 4";i:2;s:8:"Option 3";}i:0;s:8:"Option 5";i:1;s:8:"Option 1";i:2;s:8:"Option 2";i:3;s:8:"Option 4";i:4;s:8:"Option 3";}
Function:
function psort_save_order() {
global $mouldings_options;
$list = $mouldings_options['profile_element_order'];
$new_order = $_POST['list_items'];
$new_list = array();
// update order
foreach($new_order as $v) {
if(isset($list[$v])) {
$new_list[$v] = $list[$v];
}
}
$mouldings_options['profile_element_order'] = $new_list;
$mouldings_options = array_merge($mouldings_options,$mouldings_options['profile_element_order']);
// save the new order
update_option('mouldings_settings', $mouldings_options);
die();
}
add_action('wp_ajax_psort_update_order', 'psort_save_order');
As far WordPress is concerned – your multi-dimensional array is one option.
To update just part of the multi-dimensional array its necessary to retrieve the entire array, alter it accordingly and then update the entire array.
Suppose your multi-dimensional array is as follows:
And suppose you want to update the value of the ‘hello’ option from ‘world’ to ‘moon’