WordPress provides a nice, simple API for loading scripts in plugins and themes. We use the wp_enqueue_script() and wp_register_script() to instruct WordPress to load the scripts we need, and it works exceptionally well. Sometimes, however, we don’t need to just load a script, but instead we need to detect if a specific script has been enqueued already.

While working on a plugin update today, I needed to detect if a script from a specific plugin was loaded so that I could properly account for a conflict. The plugin I was working on connects WordPress to an Amazon S3 account in order to store media files in S3. The conflicting plugin provided an upload form on the frontend of the website that users can upload files with. Due to how my plugin works, the “Upload to S3” option was showing up on the frontend upload forms. For several reasons, this wasn’t desirable, so I needed a way to detect if the other plugin’s form JS was enqueued.

WordPress gives us a really nice function called wp_script_is() that lets us detect if a specific script has been enqueued, registered, printed, or is in the queue to be printed.

It works like this:

if( wp_script_is( 'script-handle', $list ) ) {
 
}

The script handle is the same as the first parameter passed to wp_enqueue_script() and wp_register_script(). The $list parameter is the list that you want to search in and accepts one of the following:

  • registered – script was registered through wp_register_script()
  • enqueued / queue – script was enqueued
  • done – script has been printed
  • to_do – script has not yet been printed

My implementation looked like this:

public static function s3_tabs( $tabs ) {
 
	if ( ! wp_script_is( 'fes_form', 'enqueued' ) ) {
 
		$tabs['s3']         = __( 'Upload to Amazon S3', 'edd_s3' );
		$tabs['s3_library'] = __( 'Amazon S3 Library', 'edd_s3' );
 
	}
 
	return $tabs;
}

It’s an exceptionally useful function. Have you used it? What kind of issue did it help you get around?