mattgadient.com

removing jQuery from WordPress

If your WordPress site doesn’t make use of jQuery on the front-end (the part visitors see), you might be tempted to remove jQuery and save your visitors that 30-40kb download.

Doing it is pretty easy. The short, dirty version (that might break things on the admin end) is as follows:

<?php wp_deregister_script('jquery'); ?>

Literally plunk that in your header.php ( before the call to wp_head() ) or in your functions file and jQuery won’t be called in the head anymore.

Of course, this can be a big problem on the admin end, particularly if it broke things/functionality you now want to use. The good news is, you can disable jQuery for only the forward-facing part of the site if you want by changing it to one of these:

<?php if (!is_admin()) wp_deregister_script('jquery'); ?>
<?php if (!is_user_logged_in()) wp_deregister_script('jquery'); ?>

The latter is probably safest, as from what I gather, is_admin() only shows true when you’re actually in the administration panel – so if you’re browsing the site with the admin bar on it may or may not load jQuery (and you may or may not need it).

A few things to note:

  • It will break a lot of plugins. Most┬ánoticeably, Lightbox/Fancybox-style stuff but there are a number of others that could be affected too.
  • Due to the above, it’s dangerous to put into a theme you’re planning to distribute (even as an option). A user might select the option and a few months later try installing a plugin and start pulling their hair out wondering why the plugin isn’t working. If you go this route, you might want to spit out constant drop-down reminders on the admin screen if they have that option enabled, reminding them that if they have problems they should turn it off.
  • WordPress will spit out messages when in DEBUG mode. Notices such as:
    • Undefined index: jquery in /home/public_html/test/wp-includes/class.wp-scripts.php on line XX
    • Trying to get property of non-object in /home/public_html/test/wp-includes/class.wp-scripts.php on line XX

Really, it’s a change for the hard-core. But if that’s you, hopefully this helps.

  • Anonymous

    Pretty cool article. Exactly what I was looking for. Thanks!