我正在使用设置API存储所有选项:
function registerSettings() {
register_setting(\'XX_theme_settings\', \'XX_theme_settings\', \'setting_validate\' );
add_settings_section(\'theme_options\', \'Theme Options\', \'theme_options_generate\', \'page1\' );
add_settings_field( \'XX_Option1\', \'Option 1\', \'text_input\', \'page1\', \'theme_options\', \'XX_Option1\' );
add_settings_field( \'XX_Option2\', \'Option 2\', \'text_input\', \'page1\', \'theme_options\', \'XX_Option2\' );
};
add_action(\'admin_init\', \'registerSettings\');
假设我想手动更新XX\\u Option2(不是使用表单,而是通过PHP),我应该怎么做?
我在想update_option(\'XX_theme_settings[XX_Option2]\', value)
, 但这行不通(?)。
最合适的回答,由SO网友:Stephen Harris 整理而成
这是因为您的选项存储为一个序列化数组,位于一行中,名称为XX_theme_settings
. 要更新一个选项,您仍然需要检索现有设置、输入适当的值并同时更新数组中的所有选项。
例如:
$my_options= get_option(\'XX_theme_settings\');//retrieve all options
$my_options[\'XX_Option2\'] = \'my new value\'; //amend value in array(s)
update_option(\'XX_theme_settings\', $my_options); //update option array