0

I'm trying to load jquery from CDN instead of loading natively with wordpress. In my functions.php I have done it like below, making sure it should only happen on the front-end:

function replace_jquery() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery2', 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js');
        wp_enqueue_script('jquery2');
    }
}
add_action('init', 'replace_jquery');

nonetheless, when I try to log in the admin area, I get a bunch of error starting with:

Notice: wp_deregister_script was called <strong>incorrectly</strong>. 
Do not deregister the <code>jquery</code> script in the administration area. 
To target the front-end theme, use the <code>wp_enqueue_scripts</code> hook. 
Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information. 
(This message was added in version 3.6.0.) in /app/public/wp-includes/functions.php on line 4204

Sometimes it does not throw this error, and sometimes it does. What am I doing wrong?

3

The error message describes your problem quite clearly:

Notice: wp_deregister_script was called incorrectly. Do not deregister the jquery script in the administration area. To target the front-end theme, use the wp_enqueue_scripts hook.

You're de-registering the script on the init hook:

add_action('init', 'replace_jquery');

This hook runs for the back and front end, but the debugger isn't smart enough to know that you're using ! is_admin() inside the function. Regardless, you should just do what the error recommends and use the wp_enqueue_scripts hook:

add_action('wp_enqueue_scripts', 'replace_jquery');

The original script will not be enqueued yet on the init hook, so attempting to de-register it on that hook won't work, as it will only get re-registered by the time the wp_enqueue_scripts hook runs.

Also, the way you're going about this is likely to cause problems, for 2 reasons:

  1. You're enqueuing jQuery 3.3.1, but WordPress uses 1.12.4. This means that WordPress itself and the vast majority of plugins are expecting 1.12.4 to be loaded, but 3.3.1 will be loaded. Code written expecting 1.X will not necessarily be compatible with 3.X, which could cause plugins to break.
  2. You're enqueueing it with a different handle, jquery2. This means that any scripts that declare jquery as a dependency will not load. This will break a great number of plugins.
  • thanks a lot! So wordpress does load the older version, and then also jquery-migrate.js. I thought I was trying to avoid multiple requests by using the latest jquery. Did not notice any problems with any other plugins, but I suppose if I do it right I can avoid them in future. Would you recommend combining jquery 1.X with jquery-migrate.js into a single file? – geochanto Feb 26 at 5:05
  • 1
    My recommendation would be to not bother with this at all and leave it alone. Loading your own jQuery is a recipe for trouble. – Jacob Peattie Feb 26 at 5:28
-1

try wp_dequeue_script( 'jquery-ui-core' ); instead of wp_deregister_script(). It may theoretically work less efficiently but the other way is not working at all.

  • sorry but this does not work because native jquery is still there when I use it. – geochanto Feb 26 at 3:57

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.