我有一个widget类(派生自WP\\u widget),它有一个方法(通过ajax调用)来更新其部分选项(元素顺序),而无需用户单击“保存”,即可设置我从构造函数中执行的处理程序:
add_action(\'wp_ajax_bgw_update_order\', array(&$this, \'update_order\'));
The
update_order
方法的作用类似于
this 问题,但它不会保存新选项。我的代码:
public function update_order() {
if (!is_admin()) die(\'Not an admin\');
if (!isset($_REQUEST[\'nonce\']) ||
!wp_verify_nonce($_REQUEST[\'nonce\'], \'section-order-nonce\'))
die(\'Invalid Nonce\');
$sections = $this->sections;
$new_order = $_POST[\'section\'];
$new_sections = array();
foreach($new_order as $v) {
if (isset($sections[$v])) {
$new_sections[$v] = $sections[$v];
}
}
$this->sections = $new_sections;
$settings = $this->get_settings();
$settings[$this->number][\'sections_order\'] = $new_sections;
$this->save_settings($settings);
print_r($this->get_settings());
die();
}
我的
update
函数执行常规操作:
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'username\'] = strip_tags($new_instance[\'username\']);
$instance[\'count\'] = strip_tags($new_instance[\'count\']);
// [...]
return $instance;
}
当我登录时
$settings
从…起
update_order
它具有正确的值:
Array
(
[3] => Array
(
[username] => username
[count] => 0
[title] => GitHub
[skip_forks] => 1
[show_octocat] => 1
[sections_order] => Array
(
[1] => Activity
[0] => Repositories
)
)
)
The
update
方法不会被调用,当我点击“保存”按钮时
$instance
(从
form
方法)没有
sections_order
钥匙
我得出的结论是save_settings
不会像我想的那样。
如何保存update_order
方法我要做的是节约sections_order
无需用户单击“保存”按钮。