Support » Plugins » Hacks » $GLOBALS['wp_scripts'] is empty

  • newleaves


    I’ve noticed this happen in the past temporarily but now it’s a permanent issue. It was happening on 4.4 and still happening on 4.5.
    I’m trying to get the packaged version of jquery with $GLOBALS[‘wp_scripts’]->registered[‘jquery’] but it’s empty. however, the list exists if i login to wordpress first and have the adminbar loaded.

    add_action('wp_enqueue_scripts', function(){
      $system_jquery = $GLOBALS['wp_scripts']->registered['jquery'];

    i’ve tried adding a priority value for the add_action.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    I’ve been unable to find any condition where ‘jquery’ is not registered on my site, which is the only way registered['jquery'] would be empty. Logged in or not, admin bar or not, anything. Are you sure your theme or some other plugin is not deregistering ‘jquery’? If it’s empty, there’s nothing to deregister anyway. Deregistering ‘jquery’ without registering another version will break things. Explicitly loading jQuery by echoing a meta link is doing it wrong.

    BTW, to check for ‘jquery’ on the backend hook ‘admin_enqueue_scripts’

    ok, i was hoping there was a common pitfall for ending up with an empty wp_scripts. i’ll dig around some more.
    I just need the version of the default registered jquery. googleapis cdn hasn’t added 1.12.3 to their line-up yet, so i’m using this snippet to prevent a missing jquery.js file from bringing the site down:

    $system_jquery = $GLOBALS['wp_scripts']->registered['jquery'];
    wp_enqueue_script('jquery', "//{$system_jquery->ver}/jquery.min.js", [], $system_jquery->ver, true);
    add_filter('script_loader_src', function($src, $handle = null) use($system_jquery){
      static $add_jquery_fallback = false;
      if ($add_jquery_fallback) { ?>
        <script>window.jQuery || document.write('<script src="//<?= $system_jquery->ver ?>.min.js"><\/script>')</script>
        <script>window.jQuery || document.write('<script src="<?= includes_url('/js/jquery/jquery.js') ?>"><\/script>')</script>
        <script>if ($ !== window.jQuery) $ = window.jQuery</script>
      <? }
      $add_jquery_fallback = ($handle === 'jquery');
      return $src;
    }, 10, 2);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘$GLOBALS['wp_scripts'] is empty’ is closed to new replies.