[wp-trac] [WordPress Trac] #29261: Enqueue script in Widget

WordPress Trac noreply at wordpress.org
Tue Aug 19 08:07:35 UTC 2014


#29261: Enqueue script in Widget
-------------------------------+------------------------------
 Reporter:  zishanj            |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Widgets            |     Version:  3.9.1
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------

Comment (by zishanj):

 Replying to [comment:1 SergeyBiryukov]:
 > `wp_enqueue_script()` should be called using an appropriate action,
 `wp_enqueue_scripts` or `admin_enqueue_scripts`.
 >
 > Could you provide a piece of code to reproduce the issue?

 Here is the sample code I am using:


 {{{
 class My_Widget extends WP_Widget {

         /**
          * Sets up the widgets name etc
          */
         public function __construct() {
                 parent::__construct(
                 __CLASS__, // Base ID
                 __('My-Widget', 'mywidget'), // Name
                 ['description' => __('my sample widget.', 'mywidget')] //
 Args
         );
         }

         /**
          * Outputs the content of the widget
          *
          * @param array $args
          * @param array $instance
          */
         public function widget( $args, $instance ) {
                 wp_enqueue_script('site-app-comp-widget', MYPLUGIN_URI .
 'includes/views/site/js/components/widget.js');

 echo $args['before_widget'];

 echo file_get_contents("test.html");

 echo $args['after_widget'];
         }

         /**
          * Outputs the options form on admin
          *
          * @param array $instance The widget options
          */
         public function form( $instance ) {
                 // outputs the options form on admin
         }

         /**
          * Processing widget options on save
          *
          * @param array $new_instance The new options
          * @param array $old_instance The previous options
          */
         public function update( $new_instance, $old_instance ) {
                 // processes widget options to be saved
         }
 }
 }}}

 I have to use wp_enqueue_script inside Widget so it can be enqueued only
 when my Widget is used. Otherwise calling it inside enqueue action will
 enqueued it regardless my widget is used or not.

 If I use this enqueue with my shortcode inside shortcode function then it
 works like a charm. It seems like this Widget is not executing before the
 wp_enqueue_script whereas the Shortcode does.

 Here is my sample code for shortcode which works well:


 {{{
 add_action('init', 'my_init');
 function my_init() {
    add_shortcode('gi_catalog', 'my_shortcode');

 }


 function my_shortcode() {

        wp_enqueue_script('site-app-comp-widget', MYPLUGIN_URI .
 'includes/views/site/js/components/widget.js');

        return file_get_contents( "myshortcode.html");
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29261#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list