WordPress has a function which lets us detect if a specific script has been enqueued, registered, printed, or is in the queue to be printed. It can be very useful for avoiding conflicts with libraries which have been already loaded by other themes or plugins.
wp_script_is( $handle, $list );
It accepts two parameters:
- $handle – The unique name of the script.
- $list – Status of the script to check. Accepts ‘enqueued’ or ‘queue’, ‘registered’, ‘to_do’, and ‘done’. This parameter is optional and its default value is ‘enqueued’.
The function returns a boolean value.
Here’s an example of its usage:
if (wp_script_is('my_script.js', 'registered')) {
return;
} else {
wp_register_script(
'my_script.js',
plugin_dir_url(__FILE__).'js/my_script.js');
wp_enqueue_script( 'my_script.js' );
}
This would check if the script named my_script.js is registered without overriding it or duplicating the copy of the library itself. If it is registered, it does nothing. If it is not, the files are then registered and enqueued.
Was this article helpful?
If you have any suggestions or questions, please leave a comment below.
Leave A Comment