我在wp_options
当前存储为多维数组的表(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";}}
我要做的是更新
profile_element_order
选项(在这些选项中)。到目前为止,一切都是这样:
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\');
数据正确地发布到DB表中(因为我可以将一些失败的尝试视为新的选项条目,如
mouldings_settings->profile_element_order
) -- 我只是很难弄清楚
update_option()
仅用于该特定选项的语法。我尝试过这样的方法(记住“Moldings\\u settings”是实际选项名称):
mouldings_settings[\'profile_element_order\']
$mouldings_options[\'profile_element_order\']
profile_element_order
但目前没有骰子。任何指点都将不胜感激!谢谢
Update这就是我现在所拥有的--ajax操作保存得很好,但当我保存插件选项时,它会复制数据库中的选项,并抛出与之前相同的错误:
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 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\');
最合适的回答,由SO网友:Stephen Harris 整理而成
就WordPress而言,您的多维数组是一个选项。
要仅更新多维数组的一部分,需要检索整个数组,请相应地修改它,然后更新整个数组。
假设多维数组如下所示:
my_options = array(
\'option_a\'=>\'value_a\',
\'option_b\'=>\'value_b\',
\'inner_array\'=>array(
\'foo\' => \'bar\',
\'hello\' => \'world\',
),
\'option_c\'=>\'value_c\'
)
假设您想将“hello”选项的值从“world”更新为“moon”
//Get entire array
$my_options = get_option(\'my_options\');
//Alter the options array appropriately
$my_options[\'inner_array\'][\'hello\'] = \'moon\';
//Update entire array
update_option(\'my_options\', $my_options);