Opened 10 years ago
Last modified 4 years ago
#29261 new defect (bug)
Enqueue script in Widget
Reported by: | zishanj | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.9.1 |
Component: | Widgets | Keywords: | needs-testing |
Focuses: | Cc: |
Description
I am trying to use wp_enqueue_script inside function widget of the Widget class. But it seems like when I print html it doesn't use the javascript functions which I enqueue before. This enqueue script works well if I use for shortcode inside shortcode and later print html. Is this a bug or there is any workaround for using enqueue inside widget function that will properly handle my html also?
Change History (6)
#2
in reply to:
↑ 1
@
10 years ago
Replying to SergeyBiryukov:
wp_enqueue_script()
should be called using an appropriate action,wp_enqueue_scripts
oradmin_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"); }
#3
@
10 years ago
I have also seen that unlike shortcode which can be called from init action, this Widget needs to be called before init action probably in plugins_loaded action.
wp_enqueue_script()
should be called using an appropriate action,wp_enqueue_scripts
oradmin_enqueue_scripts
.Could you provide a piece of code to reproduce the issue?