WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to load a special Javascript File just when the widget is loaded in the sidebar. Where do I have to put my wp_enqueue_script(...) lines?

class controller_widget extends WP_Widget {
    function controller_widget() {
        $this->color = "red";

        $this->dir = plugin_dir_url(__FILE__);
        $widget_ops = array('classname' => 'controller_widget', 'description' => __('Benutzerfreundlicher Controller zur eingrenzung von Objekten'));
        $control_ops = array('width' => 350, 'height' => 400);
        $this->WP_Widget('controller_widget', __('Controller'), $widget_ops, $control_ops);

        // At the moment I load the code here, but I want to proof that it is just loaded when the widget really is shown in the sidebar.

    function widget( $args, $instance ) {...}

Thank you!

share|improve this question

migrated from stackoverflow.com Sep 12 '11 at 21:31

This question came from our site for professional and enthusiast programmers.

Can you please give a more detailed explanation – gautham5678 Sep 12 '11 at 16:44

Put it in the widget script file itself. This method will ensure that your script is loaded i the appropriate place of the DOM.

share|improve this answer
I thaught about doing it like this, but isn't there a better solution? One that is more elegant? – user589216 Sep 12 '11 at 16:49

Your Answer


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.