Easy way to check if jQuery is already enqueued

Had a plugin wreaking some havoc today because it was overloading jquery.js with a minified, older version. Plugin authors: There’s a really simple way to check if jQuery or a jQuery library is already registered and enqueued. This covers really obscure edge cases where a plugin may have de-registered WordPress’s default scripts.

The offending code:

wp_register_script('myjquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js', true, '1.6.4', false);
wp_enqueue_script('myjquery');

The fix:

// If jQuery isn't already enqueued, register and enqueue it
if ( ! jQuery ) {
	wp_register_script('myjquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', true, '1.7.2', false);
	wp_enqueue_script('myjquery');
}

Published by

Drew Jaynes

Drew is a former Core Developer for the WordPress open source project, and he works on cool plugins like AffiliateWP, Easy Digital Downloads, and Restrict Content Pro.

Leave a Reply

Your email address will not be published. Required fields are marked *