Skip to content

Stop WordPress plugins loading jQuery

G23rd June 2014

C1 Comment

TJavaScript, jQuery, WordPress

A quick method for stopping most WordPress plugins from loading an independent instance of jQuery (and thus avoiding conflicts).

Some WordPress plugins come bundled with jQuery. In such instances, the plugin version of jQuery may load in addition to others already coded into the page, thus causing incompatibility which typically manifests itself as jQuery failing (along with all of the page features that rely on it).

I recently discovered such an incompatibility caused by Akismet, which was running jQuery 1.11.0 (my website currently runs jQuery 2.1.1). Using previously published code, I had customised my WordPress theme headers.php file to serve a self-hosted copy of jQuery 2.1.1, but Akismet was serving the older version further down the page.

As it turns out, there’s a simple piece of code that can be inserted into a theme’s headers.php file that disables any plugin from loading its own copy of jQuery (regardless of version). Just add this somewhere between the <head> tags, preferably before <?php wp_head(); ?> :

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

The code will work whether you load your own hosted copy of jQuery, a copy from Google CDN or WordPress’ default copy.

Limitations

This method will work with the majority of plugins that load WordPress’ jQuery via wp_enqueue_script (Akismet is one of these).

For the minority of plugins that don’t do it this way, you’ll need to hard-code the jQuery reference out of the plugin, but run the risk that your modification is overwritten with any plugin upgrade. My suggestion in such a case would be to review your need to use the plugin in the first instance and if the case exists for its continued use, contact the vendor and suggest a re-code.

The specific issue with Akismet

Askimet was inserting the following HTML code below the comment submission form on my WordPress article pages:

<p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="ea102df338" /></p><input type="hidden" id="_wp_unfiltered_html_comment_disabled" name="_wp_unfiltered_html_comment_disabled" value="77c6025334" /><script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>
<script type='text/javascript' src='http://code.adonline.id.au/wp-includes/js/jquery/jquery.js?ver=1.11.0'></script>
<script type='text/javascript' src='http://code.adonline.id.au/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
<script type='text/javascript' src='http://code.adonline.id.au/wp-content/plugins/akismet/_inc/form.js?ver=3.0.0'></script>
<p style="display: none;"><input type="hidden" id="ak_js" name="ak_js" value="241"/></p>

This HTML could could be tracked back to line 672 in /plugins/akismet/class.akismet.php:

public static function load_form_js() {
wp_enqueue_script( 'akismet-form', AKISMET__PLUGIN_URL . '_inc/form.js', array( 'jquery' ), AKISMET_VERSION );
wp_print_scripts( 'akismet-form' );
}

Whilst I could have commented-out the reference to jQuery in that file, the change would have been overwritten in future Akismet upgrades. (For the record, I attempted to change the code in Akismet and it worked). The wp_deregister_script method is far better.

   

Comments:

One response to “Stop WordPress plugins loading jQuery”

  • Written by Agence web Limoges on 27 June 2015:

    Thanks for sharing this. The customization of functions.php will be easier.

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.