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 am trying to pass some widget options to an external script using wp_localize_script. I know how to pass the variables and how to use them within the script, but how do I extract the widget options to pass them as an array to the script? Here is what I have currently:

class WidgetName extends WP_Widget {
    public function __construct() {

        // load plugin text domain
        add_action( 'init', array( $this, 'widget_textdomain' ) );
        add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_scripts' ) );

    }

    // This is what i'm having a problem with
    public function register_widget_scripts() {
        $options = get_option('widget-name');
        wp_localize_script('handle', 'obj-name', $options);

    }
    // The rest of the widget code goes here
}
share|improve this question

1 Answer 1

You can use the get_settings() member function:

public function register_widget_scripts() {
    $options = $this->get_settings();
    wp_localize_script( 'widget-name', 'obj-name', $options );
}
share|improve this answer
    
Thanks for the answer, @bungeshea. Any idea why this doesn't work for me? –  Yoav Kadosh Jan 14 '13 at 7:34

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.