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 created a widget that uses a jquery plugin, and I used is_active_widget to enqueue the script, it's working fine but it's also including the script even on pages that doesn't show this widget. so is there a way to only enqueue the script if this widget is showing ?

thanks in advance.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

You should be able to call wp_enqueue_script() as part of your Widget output.

Edit

Quick-and-dirty, using the bare-bones Widgets API class example:

<?php
class wpse48337_Widget extends WP_Widget {

    public function __construct() {
        // widget actual processes
    }

    public function form( $instance ) {
        // outputs the options form on admin
    }

    public function update( $new_instance, $old_instance ) {
        // processes widget options to be saved
    }

    public function widget( $args, $instance ) {
        // outputs the content of the widget
    }

}
register_widget( 'wpse48337_Widget' );
?>

Add your wp_enqueue_script() call inline, within your Widget's output - i.e. within the public function widget():

<?php    
    public function widget( $args, $instance ) {
        // outputs the content of the widget

    // Enqueue a script needed for
    // the Widget's output
    wp_enqueue_script( 'jquery-pluginname', $path, $deps );

    // Rest of widget output goes here...
    }
?>
share|improve this answer
    
Actually I'm doing that, and using is_active_widget() but it will enqueue the script on all pages if the widget is active even on one. –  Peter Apr 9 '12 at 6:58
    
Using is_active_widget() is not the same as calling wp_enqueue_script() from within your Widget output. See updated answer. –  Chip Bennett Apr 9 '12 at 11:52
    
thank you for the update, but what do you mean by quick and dirty the example or this way for using wp_enqueue_script() ? and one more off topic question please, I'm seeing a lot of these private and public before functions in widgets so what do they do ? and should we use them widgets specific for themes ? and thank you very much :) –  Peter Apr 11 '12 at 6:32
    
By "quick and dirty example", I mean that I'm referencing the standard Widget class construct, as described in the linked Codex article. I'm assuming that you've written your custom Widget using the Widgets API. If not, you should be; but nonetheless, if you need more specific guidance, we'll need to see your Widget code. –  Chip Bennett Apr 11 '12 at 11:53

If the script needs to go in the <head> section:

class Your_Widget extends WP_Widget{

  protected static $did_script = false;

  function __construct(){

    // parent::__construct() ...

    add_action('wp_enqueue_scripts', array($this, 'scripts'));

  }

  function scripts(){

    if(!self::$did_script && is_active_widget(false, false, $this->id_base, true)){
      wp_enqueue_script('your-script');
      self::$did_script = true;
    }           

  }


}

Otherwise, Chip Bennett's solution will work for queuing it in the footer.

The $did_script static variable is not required, I used it just to avoid further unnecessary calls to wp_enqueue_script if there are multiple widget instances on the page...

share|improve this answer
    
Cool idea, but this doesn't actually address the OP's question, since is-active_widget() just lets you know whether a widget has been activated (ie: dragged into one of the sidebar areas in Appearance > Widgets), as opposed to actually appearing on any given page. –  Tom Auger Aug 19 '13 at 16:15

I considered 'wp_footer' hook because this hook is executed at footer,and is probably the best way to add scripts only where the widget is used.

class Your_Widget extends WP_Widget{

    function widget( $args, $instance ) {

         add_action('wp_footer',array($this,'front_end_scripts'));

    }

    function front_end_scripts(){
       ?><script type="text/javascript">
          console.log('this works!');
        /*
          Or if you need external scripts then you may use 
          $.getScript([your-script-url] );
        */
        </script> <?php
    }



}
share|improve this answer
    
Ugh. wp_enqueue_script() please. No direct printing of JS. –  Tom Auger Aug 19 '13 at 15:57
    
I'm afraid wp_enqueue_script() will not work here since the hook besides wp_enqueue_script is fired much before the widget is actually called. –  Dipesh Kc Aug 20 '13 at 5:47
    
You can call wp_enqueue_script() any time before the wp_footer action. The widget is called during the building of the template page. You can either hook onto dynamic_sidebar action, detecting when your widget is actually displayed, or, more simply, just put your enqueue_script() right into the widget() method, as @ChipBennet recommends. –  Tom Auger Aug 20 '13 at 13:42

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.