1

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?

  • The message is rather clear: Restrict the replacement to the frontend. Plus, why would you prefer an uncacheable version of jQuery? – fuxia Nov 2 '15 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 Nov 2 '15 at 15:00
  • This version is cached for just one hour. That's nothing. – fuxia Nov 2 '15 at 15:33
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.

  • thanks , but why it is not showing error in index.php or single.php but it is showing error in wp-login.php?? is there any special reason behind it? – stlawrance Nov 2 '15 at 14:55
  • The login pages are considered "admin" pages in some ways. – s_ha_dum Nov 2 '15 at 15:03
  • thanks . So we shouldnot use cdn in small traffic site? i thought that if one site cache script from cdn say google ajax . Then other site should not download it . – stlawrance Nov 2 '15 at 15:09
  • I am sure there is debate about that, but I believe that CDNs are, as I said, over-rated for many applications. – s_ha_dum Nov 2 '15 at 15:11
  • Yes thanks . But suppose say contributor came to site . So it means the script is going to register isn't it ? so they see error?? – stlawrance Nov 2 '15 at 15:14
2

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.

  • yes it is nice :) So wordpress treats wp-login.php as a backend and things like index.php etc. are front end? – stlawrance Nov 2 '15 at 15:01
  • If you're referring to the files in the root folder, you shouldn't be touching them at all. wp-login.php is admin-like as it controls the flow of users into the authenticated areas, including the admin panel. index.php is just a routing script that hands off the request to the proper place in WP. Aside from wp-config.php, the only files you should be modifying are in your theme and plugins, unless you intend to make WP do some very non-standard things. – phatskat Nov 2 '15 at 15:09
  • @phatshak Ok Ok . But our site gives user to login register etc. Lets say they get the role contributor. They are not admin so your code run. It means they are going to see error??? – stlawrance Nov 2 '15 at 15:13
  • No, is_admin() checks to see if the page is an admin page, not if the user is an administrator. It's a little confusing, but is_admin() pretty much checks if you're on a /wp-admin/ screen or not. codex.wordpress.org/Function_Reference/is_admin – phatskat Nov 2 '15 at 15:48
1

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');
} );

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.