0

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.

1
  • chrisguitarguy is right. You shouldn't need to write a save handler. I am wondering if the problem is that your handler is interfering with the default functionality.
    – s_ha_dum
    Dec 15, 2012 at 18:37

1 Answer 1

2

Why are you writing your own save handlers for widgets? The update method your widget class will take care of this. You're saving per widget settings, exactly what the Widget API was designed to do.

<?php
class WPSE76224_Widget extends WP_Widget
{
    public function __construct()
    {
        // Create the widget options and such here, then call
        // $this->WP_Widget to let wordpress know about it.
    }

    public function widget($args, $instance)
    {
        // display the widget on the front end.
    }

    public function form($instance)
    {
        // The widget form in the admin area.
    }

    public function update($new, $old)
    {
        // Saves your data, called via AJAX. But your saving logic here.
    }
}

Since WordPress "loads" the widget class (you just register it with register_widget). Chances are an instance of of the widget class is not created when you think it is -- on ajax requests, for instance. If you do want to write your ajax handlers, you'll likely have to move that functionality outside of the widget class.

Further reading:

  1. http://codex.wordpress.org/Widgets_API
  2. http://pmg.co/how-to-build-your-own-wordpress-widgets This is an older tutorial I wrote. The "fetching the tweet" part doesn't work anymore due to changes a Twitter.
3
  • I din't include the update method because it never gets called when I do the ajax request from the client. What I want to do is to save the items order without the user clicking "save".
    – Francesco
    Dec 15, 2012 at 17:08
  • Revise your question to include that information and more about what you're trying accomplish. Were it me: a simple bit of JavaScript to setTimeout with a callback to "click" your widgets save button. Doesn't seem like something you really need to worry about, however -- just let people click save. Dec 15, 2012 at 23:10
  • I updated my question, sorry if I didn't specify what I'm trying to do from the beginning.
    – Francesco
    Dec 16, 2012 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.