I know about wp_enqueue_script. i just wanted to make sure that it applies here. I built a custom widget that uses a lot of js. Most of that js is included in a separate file that gets included from a switch statement that also runs a query. So for example, if someone chooses a slider from the form in the widget, then the widget gets the slider.php file which has the html and it has the js to run the slider. The issue with this is that when you go into the admin area, you can see all the js in the text editor for that post or page. I am using a page builder to insert the widget.

What I wanted to know. What's the best "Right way" to include the js? should I use the enqueue_script in the slider.php file or is there another way? I was thinking something in the functions.php would work best. Test for the widget to see if it's active and if slider.php was called, then wp_enqueue_script for the .js?

All my searches keep bring up how to include js in WordPress for the front end. I am looking to only include it if the page uses it.

  • 2
    Have you tried to call wp_enqueue_script() in your widget output function? – fuxia Aug 11 '15 at 2:39
  • yes, I thought about doing it that way. I just thought maybe there was another method that I didn't know about. In the End, that is what I did. this seems to have accomplished what I wanted. – Jamie Aug 11 '15 at 17:28

Your Answer

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

Browse other questions tagged or ask your own question.