3 Ways to Easily Check if a Plugin Exists in WordPress

Plugins are an integral part of WordPress and used to enhance a website’s features and functionality. While developing a website, you might require a certain piece of code to parse only when a certain plugin is active. This practice helps not only optimizing the website but also helps mitigating any prospective errors. In this article, we discuss 3 major ways we can check if a plugin is active in WordPress. First, let’s see why we might need to check if a plugin is active in a WordPress website.

To follow along this article, I’m assuming you have a basic understanding of WordPress development and are familiar with basic WordPress concepts.

Why we need to check is a plugin is active

While plugins are a very important part of WordPress websites, adding plugins does take a toll on your website. Depending on the size of the plugin, it introduces a number of additional resources that have to be loaded either from the website server or a third-party server. If a plugin is loading resources from a third-party, it means additional HTTP Requests which simply means more time for the website to load. While some optimization plugins do help optimize the page speed of your website, most of the plugins increase the payload and in turn, the page speed of a website.

There are other instances where you might want to check for a plugin before running a certain function. For instance, you might want to disable the gallery block when a third-party gallery plugin is installed. In such a scenario, we check if the plugin is installed and disable the gallery block accordingly.

Now then we have discussed the situations where we might need to check for a plugin, let’s go ahead and take a look at how to check if a plugin is active.

How to check if a plugin is active in WordPress

There are many methods for checking active plugins, but I’m going to discuss 3 of the most common ones.

is_plugin_active

Using is_plugin_active  is the most straight forward method of checking if a plugin is active in WordPress. The basic syntax of this function is as follows –


This function basically requires one argument, the path to plugin’s main file. Let’s see this function at work with an example –

So, in the above code, we are enqueuing products.js file in the “js” folder in our theme directory only when the “WooCommerce” plugin is active. We have used the wp_enqueue_script function to enqueue the products.js file. You can read more about this function in the official documentation.

Note that we have hooked the function my_theme_enqueue_scripts to admin_enqueue_scripts action hook. This means we this function is being run in the back-end. This is one “gotcha” of this function. The is_plugin_active  function is mainly built to run in the back-end. In order to make it run on the front-end, we need to make a little change.

In the snippet above, we are enqueuing the my_theme_enqueue_scripts function to wp_enqueue_scripts . This means this function is being run on the front-end. We perform a similar check for the WooCommerce plugin but in this case, there is an additional line of code –

include_once ABSPATH . 'wp-admin/includes/plugin.php';
We have to include plugin.php from the wp-includes folder of WordPress. This file is needed if want to detect a plugin file. Now, our products.js file will only be loaded on front-end if WooCommerce is active.
Under the hood, this function checks all the plugins in wp-content/plugins folder. In case of must-use plugins, this function doesn’t work and always returns false .

“class_exists” function

This is another function we can use to check if a plugin is active in a WordPress installation. Basically, what this function does is check for the presence of a class in the code base. The syntax of this function is as follows –

Extending from our previous example, we can use class_exists to check for WooCommerce plugin before enqueuing the products.js file –

In the snippet above, we are checking for the WooCommerce class using the class_exists function. WooCommerce declares this class in it’s code base. The benefit of this method over is_plugin_active  is that we don’t have to include any other file. class_exists is a native PHP function.

One drawback you might encounter with this method is you’ll have to manually check in the plugin files if the plugin has declared a class or not. In some cases, plugins prefer to use functions rather classes. In such a case, our next method will come in handy.

“function_exists” function

Here is another function using which we can check if a plugin is active or not. The syntax of this function is as follows –

This function accepts one argument, the function name we need to check for. Let’s continue with our previous example and implement this method in it.

In the snippet above, we are using the function_exists function to check if WC  function exists or not. If we go through WooCommerce plugin’s main file, we note that it has declared the WC function to register the instance of WooCommerce.

Conclusion

So that was it! In this article, we saw how we can check if a plugin is active in WordPress using 3 different methods. In my opinion, the is_plugin_active is the most robust. It would work in almost all conditions albeit it’s not a super custom WordPress setup. In other methods, you need to sift through the plugin’s code or documentation.

Hope you were able to learn something new in this article. For more such WordPress tips, tricks and tutorials, do check out our blog where we regularly post insightful articles related to WordPress and Web Development. See you in the next article!

Meet the Author

Divjot Singh

Divjot is an experienced WordPress developer and technical writer with more than a decade of experience in WordPress Development. Whenever he's not creating amazing WordPress experiences, he can be found on the road with his bike.

Leave a Reply

Your email address will not be published. Required fields are marked *