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:

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

The fix:

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

Published by

Drew Jaynes

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

Leave a Reply