WordPress: Recognizing That A Plugin Is Activated

As I upgraded the site to WordPress 1.5, I had some problems with plugins. Namely for the AuthImage plugin I had half the changes in (with the plugin deactivated) and noticed that no matter whether the plugin was activated or deactivated, the site attempted to validate the authorization code anyway.

This also meant that I couldn’t, at a future date, deactivate the plugin without going back and removing the custom code I had to insert per the installation instructions to the wp-comments-post.php and the wp-comments.php file in my current theme in order to remove the error that subsequently happened when the call to checkAICode happened.

So, I worked up a solution to recognize the plugin. I added the following function to my my-hacks.php:

# Function to find out whether the authimage plugin is actually
# activated.
                                                                                                                             
function isAuthImageActive() {
    $returnValue = false;
                                                                                                                             
    $plugin_list = get_settings('active_plugins');
                                                                                                                             
    if (array_search('authimage.php', $plugin_list) !== false) {
        $returnValue = true;
    }
                                                                                                                             
    return($returnValue);
}

This allows me to query the system with a function that is available all of the time in order to find out whether the AuthImage plugin was actually activated or not.

I then wrapped all of the code that the installation instructions for the plugin told me to add to the code with the following block:

if (isAuthImageActive()) {
     custom code here
}

Now when I deactivate the plugin through the admin interface, the whole thing goes away.

Now, I know this isn’t rocket science and there is definitely a more general way to do this, but until I need to generalize it more I’m going with this solution. Remember, for this to work you have to enable my-hacks.php support.

One thought on “WordPress: Recognizing That A Plugin Is Activated

  1. I’ve made your plugin check more generic. It now takes a plugin name parameter (name of the plugin *.php file) and compares it to the plugin array.

    # Function to find out whether the a plugin is activated
    function isPluginActive($name) {
    $plugin_list = get_settings(‘active_plugins’);
    return (array_search($name, $plugin_list));
    }

    Now, any plugin that requires changes to the template files will look like this:

    if (isPluginActive(‘countdown.php’)) {
    // custom code goes here
    }

Comments are closed.