Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 at 18:37
add comment

1 Answer

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.
share|improve this answer
    
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 '12 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. –  chrisguitarguy Dec 15 '12 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 '12 at 10:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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