How to Check If WooCommerce Plugin Is Active

check if woocommerce plugin is activeWhen developing WooCommerce plugins you may occasionally need to check if WooCommerce plugin is active. This is a common need when you have a plugin that depends on WooCommerce as the base plugin.

For example, when I was developing the WooCommerce Redirect After Checkout plugin, I needed to check if WooCommerce plugin is active before I can activate the WooCommerce redirect plugin.

Today, I want to share with you the best and easy way you can use to check if WooCommerce plugin is active before you proceed to the other actions in your plugin.

When to Check If WooCommerce Plugin is Active?

There are several cases when you want WooCommerce plugin to be active before you can activate another plugin.

For example, when you have added your WooCommerce plugin menu under the main WooCommerce plugin menu, you may need to ensure that the WooCommerce plugin is active before you can proceed.

The best way to check if WooCommerce plugin is active is using the conditional tags and adding a notice at the header section of the admin screen if the plugin is not active to let the user understand they need to activate the WooCommerce plugin before they can proceed with the current action.

Check If WooCommerce Plugin Is Active

In your plugin base file, you should add the following code to check and notify your plugin users that WooCommerce plugin should be activated before they can proceed with the current action.

/**
 * Require WooCommerce to be active and if it isn't, display the admin notice. 
 */

if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
	add_action( 'admin_notices', 'njengah_admin_notice' );
}

You can now add a callback function to the action hook in there to display the message to the user that they need to activate the WooCommerce plugin before they can proceed.

This add action hook can be modified to display any other custom message you want your users to see in the location you want in the admin dashboard.

How to Check If WooCommerce Plugin Is Active

Conclusion

In this post, I have shared with you the best way to check if WooCommerce plugin is active before you can perform another action in your plugin. You should add this code to your plugin or WooCommerce theme functions.php file and customize the message in the callback function of the action hook to suit your specific message.

Similar Articles


Customer Reviews

NJENGAH

Customer Reviews

Jeffrey Todd 04/07/2020

Exceptional Plugin & Service

I'm in the process of building my new website, and I had a question about changing some text within the WooCommerce plugin. I was spinning my wheels trying to figure it out. Joe reached back and worked with me - going above and beyond! - until I fully understood how to effectively change what I wanted. How can you ask for more than that? I highly recommend WooCommerce (and Joe!), the plugin and service are exceptional!
-Jeffrey Todd (MyLifeChart.com)