WordPress: check if jQuery has been already loaded by a theme or plugin

WordPress: check if jQuery has been already loaded by a theme or plugin

By using the WordPress wp_script_is() function we can avoid duplicate versions of the jQuery library.

Sometimes WordPress plugins are broken for a very simple reason: they forget to check if jQuery has been already loaded by the current theme so they override it or duplicate the copy of the library itself. Fortunately, there's a quick solution to this problem: use the wp_script_is() function to make sure that only a copy of jQuery is currently running.

This WordPress function returns a boolean value which determines if a script has been registered, queued, printed, or is waiting to be printed. You have only to pass the name of the library you want to test as the first argument of the function:

if(wp_script_is('jquery')) {

    // do nothing

} else {

    // insert jQuery

}

The name of the library must be all in lowercase letters.