theme development - What does "Do not deregister the jquery script in the administration area" mean?

admin2025-01-07  5

We all know the advantage of using cdn caching etc.. For that sake i deregistered the script and registered jquery with cdn link . But the problem is that it is showing error like this

Yes Debugging in wordpress is turned on.

It shows error like this .

Notice: wp_deregister_script was called incorrectly. Do not deregister the jquery script in the administration area. To target the frontend theme, use the wp_enqueue_scripts hook. Please see Debugging in WordPress for more information. (This message was added in version 3.6.) in D:\learnnepal\wp-includes\functions.php on line 3622

But i think code is fine .

WHen i remove the following lines of code that notice don't show ?

add_action( 'wp_enqueue_scripts', function(){
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', '.min.js', array(), null, false );
    wp_enqueue_script( 'jquery');
});

Is there any problem with the code?

Clearly turning off debugging is workaround but very bad practise isn't it?

We all know the advantage of using cdn caching etc.. For that sake i deregistered the script and registered jquery with cdn link . But the problem is that it is showing error like this

Yes Debugging in wordpress is turned on.

It shows error like this .

Notice: wp_deregister_script was called incorrectly. Do not deregister the jquery script in the administration area. To target the frontend theme, use the wp_enqueue_scripts hook. Please see Debugging in WordPress for more information. (This message was added in version 3.6.) in D:\learnnepal\wp-includes\functions.php on line 3622

But i think code is fine .

WHen i remove the following lines of code that notice don't show ?

add_action( 'wp_enqueue_scripts', function(){
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', array(), null, false );
    wp_enqueue_script( 'jquery');
});

Is there any problem with the code?

Clearly turning off debugging is workaround but very bad practise isn't it?

Share Improve this question edited Nov 2, 2015 at 14:50 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Nov 2, 2015 at 14:43 stlawrancestlawrance 4111 gold badge4 silver badges11 bronze badges 3
  • The message is rather clear: Restrict the replacement to the frontend. Plus, why would you prefer an uncacheable version of jQuery? – fuxia Commented Nov 2, 2015 at 14:51
  • @toscho thanks , but isn't cdn hosted jQuery is good ? Suppose if a person load jquery in another site thorough that google than it will be cached . And when that visitor visit our website they don't need to download? confused :( – stlawrance Commented Nov 2, 2015 at 15:00
  • This version is cached for just one hour. That's nothing. – fuxia Commented Nov 2, 2015 at 15:33
Add a comment  | 

4 Answers 4

Reset to default 2

Based on the error...

add_action( 'wp_enqueue_scripts', function(){
    if (is_admin()) return; // don't dequeue on the backend
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', array(), null, false );
    wp_enqueue_script( 'jquery');
});

Honestly, unless you have tremendous traffic over a broad geographic area, I'd say that CDNs are grossly over-rated. I've watched the hangup on sites I've managed and very often the bottleneck is the CDN-- I'm looking at you Google. So, this may not be a solution worth implementing.

Second, dequeueing Core scripts is a dangerous game. Plugins and themes depend upon those scripts. If you load a different version than the one expected scripts can fail.

Clearly turning off debugging is workaround but very bad practise isn't it?

Production or development? Debugging should be off on a production server and turned on only while debugging.

As the error notes, you really don't want to deregister the jquery script in the administration area. What you could do to avoid the notice:

if ( ! is_admin() ) {
    add_action( 'wp_enqueue_scripts', function(){
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', array(), null, false );
        wp_enqueue_script( 'jquery');
    });
}

The reason you shouldn't do the deregister in the admin area is that a lot of the WP core functionality for the admin section is reliant on JavaScript, and the WP team has coded that functionality to work specifically with the version of jQuery that ships with WordPress. While there shouldn't be any difference in functionality, if there was something missing or broken in a different jQuery version it could render your admin area unusable.

If you use the right hook then it shouldn't complain:

if ( is_admin() ) {
    $hook = 'admin_enqueue_scripts';
} elseif ( 'wp-login.php' === $GLOBALS['pagenow'] ) {
    $hook = 'login_enqueue_scripts';
} else {
    $hook = 'wp_enqueue_scripts';
}
add_action( $hook, function() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', array(), null, false );
    wp_enqueue_script( 'jquery');
} );

I have encountered the same error on the login page. It is necessary to check also if it is the login page.

The up-to-date version of the code is the following:

    add_action( 'init', function(){
    
        // Check if it is admin or login page
        if (is_admin() || is_login()) return;
    
        // Ditch WP's version.
        wp_deregister_script( 'jquery' );
        wp_deregister_script( 'jquery-migrate' );
    
        // Add jQuery from Google's CDN.
        wp_register_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js', false, '3.7.1', true );
        wp_enqueue_script( 'jquery' );
        wp_add_inline_script( 'jquery', 'window.jQuery || document.write("<script src=\"' . esc_url( get_site_url() ) . '/wp-includes/js/jquery/jquery.min.js?ver=3.7.1\" type=\"text/javascript\"><\/script>")' );
    
        // Add jQuery migrate from jsDelivr's CDN.
        wp_register_script( 'jquery-migrate', '//cdn.jsdelivr.net/npm/[email protected]/dist/jquery-migrate.min.js', false, '3.5.2', true );
        wp_enqueue_script( 'jquery-migrate' );
        wp_add_inline_script( 'jquery-migrate', 'window.jQuery || document.write("<script src=\"' . esc_url( get_site_url() ) . '/wp-includes/js/jquery/jquery-migrate.min.js?ver=3.4.1\" type=\"text/javascript\"><\/script>")' );
    });
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253972a167.html

最新回复(0)