wp enqueue script - Wordpress error when replacing local jQuery by externally-hosted

admin2025-06-02  3

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', '.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="">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?

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/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/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?

Share Improve this question asked Feb 26, 2019 at 2:33 geochantogeochanto 13115 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748866160a314372.html

最新回复(0)