Jump to content
Sign in to follow this  
dil_bert

How to check is plugin active in wordpress? which methods do we have?

Recommended Posts

hello dear community, 

 


How to check is plugin active in wordpress. I've installed a plugin in directory: /wp-content/plugins/my_plugin  and the plugin file is in /wp-content/plugins/my-plugin.php 

I tried this and - note: i  know that plugin is active.:

    if (is_plugin_active(get_home_url() . '/wp-content/plugins/my-plugin.php')){
    echo 'plugin is active';
}else{
    echo 'plugin is not active';
}

 

The above code echoes that 'plugin is not active' but plugin is active. I think the problem is my dir to plugin address. i guess that i do have to look if plugin is active and to check if plugin is enabled or not? 

what did i do to solve the issue: To do so i tried to gather all the methods to do so - i read all the things i could get to this topic.  i tried to find all the methods to do that job. 

 

There are  several ways by which we can check plugin is active or not.

the first way: we can use is_plugin_active function to check plugin active or not.

If the plugin has got a class then we can check like below

 

if (class_exists('my-plugin')) 
{
echo 'our code';
}

 

The above class related to my-plugin . The above code check if class exist then only our code inside the bracket will execute.


we can also try to check plugin is Active or Not

$pluginList = get_option( 'active_plugins' );
$plugin = 'PLUGIN_DIR/FILE_NAME.php'; 
if ( in_array( $plugin , $pluginList ) ) {
    // Plugin 'my-plugin' is Active
}



third way: below code for simplicity

if ( is_plugin_active( 'PLUGIN_DIR/my-plugin.php' ) ) {
  //plugin is activated
} 

and we can simply use the WordPress default function.

we can simply pass the Path to the plugin file relative to the plugins directory.

 

$pluginPath =  '/my-plugin.php';
if(is_plugin_active( $pluginPath )){
    echo "Plugin is activated";
}else{
    echo "Plugin is not activated";
}


furthermore we have the link to the reference: https://developer.wordpress.org/reference/functions/is_plugin_active/

is_plugin_active( string $plugin ) 
function is_plugin_active( $plugin ) {
    return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
}


 

question: are there more ways to check if the plugin is active? any suggestion is welcome :)


above all: i am so happy to be here in this  great place.

plz keep up the great work - this place is one of the best places for help in all php things and much much more!!!

many thanks to all the supporters here!!! you guys rock!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.