Enter

WordPress 101 / 4 min read

What Is the is_plugin_active WordPress Function and How to Use it?

colorful programming code on a black screen

is_plugin_active is a WordPress function that checks whether a specific plugin is active. Determining whether a plugin is active can help you create custom functions that only execute when a specific plugin or a set of plugins is active.

Let’s explore how is_plugin_active works and how to use it on your WordPress website.

Key Takeaways

  • is_plugin_active is a function that returns true when the plugin is active and false when it’s inactive.
  • Depending on whether the plugin is active, you can execute conditional functions that enrich your website.

What Is the is_plugin_active Function in WordPress?

is_plugin_active is a built-in utility for checking whether a specific plugin is currently active on your site.

This function can be useful when you’re developing custom themes or plugins and need to ensure that certain plugins are active before executing specific code. Essentially, it helps you avoid conflicts or errors by verifying plugin status, allowing your code to adapt accordingly.

is_plugin_active works by taking the plugin’s directory and main .php file as its parameter and checking it against the active plugins in the WordPress installation. If the plugin is active, the function returns true; otherwise, it returns false.

This function doesn’t work for must-use plugins, as they cannot be activated by traditional means. As a result, must-use plugins will always return false when tested with this function.

How to Use the is_plugin_active Function in WordPress?

The is_plugin_active function works as follows:

is_plugin_active( string $plugin ): bool

There’s only one argument: the string $plugin. $plugin is the path to your plugin’s main .php file, relative to the plugins folder.

For example, if you want to check whether the Contact Form 7 plugin is active, you need to know the path to its folder and the name of its main .php file. In this case, your code would look like this:

is_plugin_active('contact-form-7/contact-form-7.php');

To know the name of the plugin’s folder and main .php file you may need to access your site’s path via cPanel or FTP. 

Now that we know what is_active_plugin does, let’s move on to an example.

Example of How to Use is_plugin_active in WordPress

Let’s explore a simple example of how to use is_plugin_active to conditionally program behavior on your WordPress website.

In this case, we’ll use the function to display a textbox only if the Contact Form 7 plugin is active. If Contact Form 7 is active, the textbox will appear and declare it’s active. If Contact Form 7 is inactive, the textbox will declare it inactive.

Here’s the code, which you should add to functions.php:

if ( ! function_exists( 'check_plugin_state' ) ) :
	function check_plugin_state () {
		/* Checks if Contact Form 7 is active  */
		if ( is_plugin_active ( 'contact-form-7/wp-contact-form-7.php' ) ) {
			// If it's active, display a textbox declaring it's active
			echo '<p style="position: absolute;
			font-size: 25px;
			top: 180px;
			right: 100px;
			padding: 10px;
			background-color: #000000;
			color: #FFFFFF;">Contact Form 7 is active. Don\'t panic!</p>';
		} else {
			// If it's inactive, display a textbox declaring it's inactive
			echo '<p style="position: absolute;
			font-size: 25px;
			top: 180px;
			right: 100px;
			padding: 10px;
			background-color: #000000;
			color: #FFFFFF;">Contact Form 7 is inactive.</p>';
		}
	}
endif;

add_filter( 'wp_footer', 'check_plugin_state' );

// Ensure the function is_plugin_active is available
if ( ! function_exists('is_plugin_active')) {
    require_once(ABSPATH . 'wp-admin/includes/plugin.php');
}

Here’s how a blog post looks when Contact Form 7 is active:

A WordPress blog post. A black textbox reads, "Contact Form 7 is active. Don't panic!"

And here’s what it looks like when it’s inactive:

A WordPress blog post. A black textbox reads, "Contact Form 7 is inactive"

We’ve successfully added conditional behavior to our WordPress site using is_plugin_active.

Use is_plugin_active on Your WordPress Site

The is_plugin_active function allows you to check whether a plugin is active. You can use this function to program conditional behavior on your website, such as displaying specific content only when a specific plugin is active.

Hopefully, you can start using is_plugin_active to gain more control over your WordPress website moving forward.

If you found this post useful, read our blog and resources for more insights and guides!