0

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
}
0

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 );
}
  • Thanks for the answer, @bungeshea. Any idea why this doesn't work for me? – Yoav Kadosh Jan 14 '13 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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