- Blogs
- WordPress 101
- What Is the is_plugin_active WordPress Function and How to Use it?
WordPress 101 / 4 min read
What Is the is_plugin_active WordPress Function and How to Use it?
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:
And here’s what it looks like when it’s 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!
Related Articles
How to... / 5 min read
How to... / 5 min read
How to Test if Your Current WordPress Page Is a Custom Post Type​?
If you're customizing your site and performing specific actions for certain content, it may be useful to test if your current WordPress page is a custom post type. This way,…
Read MoreWordPress 101 / 6 min read
WordPress 101 / 6 min read
Storyblok or WordPress to Build Your Site? Our Experience as an Agency
With new CMSs popping up every year, you may wonder whether your site would benefit from the newer, up-and-coming Storyblok or WordPress, the CMS veteran that powers nearly half the…
Read MoreHow to... / 5 min read
How to... / 5 min read
How to Add HTML to the Body of a WordPress Post or Page
So, you want to add your own custom HTML to the body of your WordPress posts and pages, huh? That's great. Adding custom HTML code is an excellent way to…
Read MoreHow to... / 7 min read
How to... / 7 min read
How to Use .htaccess to Block an IP Address From the WordPress Login Page
.htaccess (hypertext access) is a configuration file used to add or change features on websites hosted using Apache Web Server, a free and open-source web server software. The .htaccess file…
Read MoreHow to... / 5 min read
How to... / 5 min read
Can You Hide Your WordPress Site While Editing it?
Sometimes, you want to edit your WordPress site but keep the changes private until you’re ready to make them public. If that’s what you need right now, you may be…
Read More