I have a widget class (derived from WP_Widget) that has a method (called via ajax) to update part of its options(the elements order) without the user to click "Save", to set the handler I do from the constructor:
add_action('wp_ajax_bgw_update_order', array(&$this, 'update_order'));
The update_order
method does something similar to this question but it does not save the new options. My code:
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();
}
My update
function does the usual stuff:
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;
}
When I log $settings
from update_order
it has the right value:
Array
(
[3] => Array
(
[username] => username
[count] => 0
[title] => GitHub
[skip_forks] => 1
[show_octocat] => 1
[sections_order] => Array
(
[1] => Activity
[0] => Repositories
)
)
)
The update
method doesn't get called and when I hit the "Save" button $instance
(logged from the form
method) doesn't have a sections_order
key.
I come to the conclusion that save_settings
doesn't do what I think it does.
How can I save the options from the update_order
method? What I'm trying to achieve is to save sections_order
without the user to click the "Save" button.