[wp-trac] [WordPress Trac] #48098: ReferenceError: jQuery is not defined even if it is enqueued

WordPress Trac noreply at wordpress.org
Sat Sep 21 14:56:10 UTC 2019


#48098: ReferenceError: jQuery is not defined even if it is enqueued
-------------------------------+------------------------------
 Reporter:  KestutisIT         |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Script Loader      |     Version:  5.2.3
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:  javascript
-------------------------------+------------------------------

Comment (by KestutisIT):

 It is registered via plugin's `registerScripts` methods in AssetLoader,
 and it registers the script 'init' and enqueues after 'add_shortcode'
 call. The enqueuing on 'add_shortcode' is important, because the we save
 network resources, and plugin scripts are not loaded on the page where the
 is no plugin shortcode used:

 {{{#!php
 <?php
 class MainController
 {
     public function run()
     {
         // <...>
         add_action('init', array($this, 'runOnInit'), 0);
         // <...>
     }

     public function runOnInit()
     {
         //<..>
         // Register front-end scripts
         $objAssetController->registerScripts();
     }
 }
 }}}

 I also did another experiment:
 1. I tested with 'TwentySixteen' theme - it works as expected.
 2. I tested with 'TwentyNineteen' theme - it works unexpectecly.

 Appears that TwentySixteen was using jQuery in theme for keyboard
 navigation or so, so it was not raising issues for the plugin, as jQuery
 got in the header.

 We need to solve this somehow, and force and say by out plugin that it
 must be loaded in header.
 And expected behavior is that if I run 'enqueue_script' either WordPress
 should give me a warning that the given script is NOT (yet) registered, or
 enqueue it. While now it does not gives me any warning but **acts
 unexpectedly**. This confuses me as a developer of the purpose of
 **enqueue_script** as it does not look reliable then with code.

 The full source code of `MainController` is in GitHub, if you want to take
 a deeper look:
 https://github.com/SolidMVC/Deals/blob/master/Controllers/MainController.php
 (and on W.org SVN as well of the plugin as `/deals/`).

 Replying to [comment:1 SergeyBiryukov]:
 > Replying to [ticket:48098 KestutisIT]:
 > > So I have a template in a plugin with:
 > >
 > > {{{#!php
 > > <?php
 > > // Scripts
 > > wp_enqueue_script('jquery');
 > > ?>
 > > <div class="great-plugin-wrapper">
 > > <!--- BUNCH OF HTML -->
 > > </div>
 > > <script type="text/javascript">
 > > jQuery(document).ready(function() {
 > >     jQuery('.responsive-deals-slider').slick({
 > >     // Bunch of javascript
 > >     });
 > > });
 > > </script>
 > > }}}
 >
 > Thanks for the report! When exactly this code runs?
 >
 > If it's not hooked to the `wp_enqueue_scripts` action as
 [https://developer.wordpress.org/reference/functions/wp_enqueue_script
 /#more-information recommended in the documentation], then it's too late
 for jQuery to be printed in `wp_head()`, so it's only printed in
 `wp_footer()`.
 >
 > > And I'm getting 'ReferenceError: jQuery is not defined' error in
 console even if it is enqueued in HTML before using.
 > > In source code it says jQuery is loaded late in footer
 >
 > Note that `wp_enqueue_script( 'jquery' )` does not print the script
 right away, it only enqueues the script to be printed later by
 `wp_print_head_scripts()` or `wp_print_footer_scripts()`.
 >
 > > Does something changed in WordPress in recent months regarding this,
 and why so? It was working at least 6 months ago.
 >
 > As far as I know, nothing has changed in the script loader in the last
 few years in that regard.

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


More information about the wp-trac mailing list