1

Because I want to use jQuery 2.1 instead of 1.11, I first have to de-register jQuery. Then register it with the new values. :

add_action( 'wp_enqueue_scripts', 'add_scripts' );

function add_scripts() {
    wp_deregister_script('jquery');
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js', false, '2.1.0', false);
    wp_enqueue_script('jquery');
}

According to the manual, this should load the script in the header. But it's loaded in the footer. Why is that?

I'm asking because I have a plugin that needs to have some stuff in the header. Since jquery isn't loaded until the footer, it throws some errors.

PS. If I do the following:

    wp_deregister_script('jquery');
    wp_register_script( 'jquery-2', 'http:...');
    wp_enqueue_script('jquery-2');

WP will not load any content at all - it completely breaks. Has anyone else experienced that?

  • Assuming you're hooking into 'wp_enqueue_scripts', have you tried disabling all plugins, even the one you need? I tested both solutions on a vanilla install without any plugins active, and they work. – Blaine Nov 8 '15 at 13:58
3

In theory the code you wrote should work, in the practical application you are missing some good practices which it might impact on how your code is working, so it's better run through a bit of troubleshooting to tick off everything from the checklist.

By de-registering jquery you are taking away both the jQuery core and the jQuery Migrate javascripts, probably what are you trying to do is to deregister just the core and keep the jQuery Migrate on.

jQuery Migrate consists in old legacy jQuery code which ports all the functionality of legacy stuff onto the new version of jQuery.

On the same page of the Codex you linked if you scroll down you can find a list of handlers of integrated javascripts you can de-register.

As result you have to to deregister jquery-core instead of jquery to keep some of the functionalities intact, this includes plugins they might some legacy jQuery stuff to run.

You don't need to append "false" at the last argument since it's already false by default, I also suggest to drop your protocol, this ensures jQuery will be always served to the browser if you want to switch to https at a later time.

As a final touch, namespacing is very important when you are coding in a big environment where other's people code runs along with yours. add_scripts() seems a bit too generic for me, if you are writing your function inside the functions.php file try to add the name of your theme, or write something very unique so you are sure the same function doesn't get collide in the future with a function coming from a plugin or something else in your working files.

add_action( 'wp_enqueue_scripts', 'YOURTHEMENAME_scripts' );

function YOURTHEMENAME_scripts() {
    wp_deregister_script('jquery-core');
    wp_register_script('jquery-core', '//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js', false, '2.1.0');
    wp_enqueue_script('jquery-core');
}

Please note that the loading order of Wordpress is the following:

Core -> Plugins -> Theme

If you are trying to de-register jQuery from a custom plugin then possibly your theme is taking over and messing around with the scripts queue.

If after all of this you're still getting the Javascript into the footer then there's something horribly wrong going on into your header.php or footer.php files: one of the rookie reasons could be the location of wp_head() and wp_footer() specific hooks in your theme, I've seen this so many times which it could be easily a possibility.

wp_head() is a hook which adds for you meta tags, the css queue and the js queue with the last flag argument set to "false".

wp_footer() is the hook which cares about adding to you the rest of the JS with the last flag argument set to "true" and extra markup generated from various plugins (and for the admin bar from the WP core).

Do a double check about the location of your hook functions, if you have wp_head() in your footer then it could be easily the reason why your javascript queue is being added to the footer.

  • Please explain your answer in more detail. Many people who come here for help know very, very little about WordPress or programming in general. – s_ha_dum Nov 8 '15 at 16:03
  • 1
    True, mea culpa. I expanded my answer a little bit, hope clarifies a bit better now. – MacK Nov 8 '15 at 22:57
  • 1
    Wow, thanks @MacK for the lengthy answer. I've worked with WP since 2008, so hopefully I'm still not a rooky ;-) I'm not sure what I did, maybe because I was working on solving another "this should not be a bug" bug, but now it's working correctly. Except from using jquery-core (that's new to me), I'm already doing everything you say. But stay tuned for my next question, because that is also another "this should not be a bug" bug :-) – Steven Nov 9 '15 at 9:45
  • 1
    PS. This is interesting, If I do wp_deregister_script('jquery'); and then do wp_register_script( 'jquery-core','http...) -> wp_enqueue_script('jquery-core'), jQuery 2.1 is not loaded and the default 1.11 is loaded. – Steven Nov 9 '15 at 9:51
  • 1
    No offense to you @Steven, I have been though several WordPress through my last years and I have seen almost everything. Static metatags, static CSS and JS on header, wp_head on footer "to boost up the site", the base style.css without header comments, everything on single.php wrapped up in ifs... Name it and probably I have seen it, so better instruct people to stop this happening :p – MacK Nov 9 '15 at 12:56
1

No 1: you have to put your jquery deregister etc. Stuff into a function that is called with the wp_enqueue_scripts hook. Do it like this:

add_action('wp_enqueue_scripts','jquery_2_loader');

function jquery_2_loader(){
//insert your jquery deregister and Register
}

Second question: many Plugins rely on enqueuing jquery. So if you deregister it, those Plugins can't use it anymore.

  • This will work, and you also need go use a different handle than 'jquery' which is a reserved name that would load the default WP jQuery. – thomascharbit Nov 8 '15 at 14:02
  • It's what I'm already doing. I'll update my Q. – Steven Nov 8 '15 at 17:17
0

You are correct that passing false to the wp_register_script function call should ensure the script is placed in the page's header. Do you have any other plugins that may be modifying your scripts' location? For instance, plugins like Autoptimize will try to "intelligently" place scripts in the footer to speed loading when possible.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.