Determine the status of a script - WordPress
When making new plugin or customizing WordPress themes in deep, we want to check that if a script has been registered, enqueued, printed, or is waiting to be printed.
This method is very useful to avoid conflicts between scripts during registering/enqueing scripts in our plugins and themes.
The WordPress have a default function to check the status of script.
wp_script_is( $handle, $list );
Parameters
- $handle :- Name of the script (string & required).
Default value: None
-
$list :- The list to query (string & optional).
registered
- script was registered through wp_register_script()enqueued
/queue
- script was enqueueddone
- script has been printedto_do
- script has not yet been printed
Default: enqueued
Return Values
True or false.
Example 1
$handle = 'fluidVids.js';
$list = 'enqueued';
if (wp_script_is( $handle, $list )) {
return;
} else {
wp_register_script( 'fluidVids.js', plugin_dir_url(__FILE__).'js/fluidvids.min.js');
wp_enqueue_script( 'fluidVids.js' );
}
Example 2
On the below example, we are going to check that if jQuery is printed & if so, we will add a custom jQuery snippet to our footer from our theme’s functions.php
.
function myscript() {
if( wp_script_is( 'jquery', 'done' ) ) {
?>
<script type="text/javascript">
// script dependent on jQuery
</script>
<?php
}
}
add_action( 'wp_footer', 'myscript' );
Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.
Recent Posts
- Disabling Payment Methods in WooCommerce Based on Conditions
- How to Update Product Quantity in WooCommerce Using Custom Code
- Dynamically Generating a Table of Contents in WordPress
- Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
- Understanding the Impact of git reset --hard Command
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.