How to check another plug-in exists and get its details

If you’re writing an add-on for one of your (or someone else’s) plug-in you want to make sure that that plug-in is active (and quite often, if it’s a sufficiently new version).

Is the plug-in active?

You can get tell if if a plug-in is active by using is_plugin_active($plugin) where $plugin is the sub-directory/file of the plug-in.

Getting the plug-in details

To get the details of that plug-in you could use get_plugin_data() but rather than taking the sub-directory/file as an argument (as above), it requires the path to the plug-in file. I’m not sure of way of doing this without relying on some constants – so an alternative is to use get_plugins().

get_plugins() returns an array, with the plug-ins as keys (more exactly, the sub-directory/file) and the plug-in’s details (including version) as a value. As an example, take my Event Organiser plugin.

 $plugin = 'event-organiser/event-organiser.php';
 if( is_plugin_active($plugin) ){
      $plugins = get_plugins();
      $plugin_data = $plugins[$plugin];
      print_r($plugin_data);
 }

This prints out something like the following:

Array ( 
 [Name] => Event Organiser 
 [PluginURI] => http://wp-event-organiser.com
 [Version] => 1.4 
 [Description] => Creates a custom post type 'events' with features such as reoccurring events, venues, Google Maps, calendar views and events and venue pages 
 [Author] => Stephen Harris 
 [AuthorURI] => http://stephenharris.info
 [TextDomain] => ''
 [DomainPath] => ''
 [Network] => ''
 [Title] => Event Organiser 
 [AuthorName] => Stephen Harris 
)

Putting it all together

Let’s suppose we have a plugin A (this is the ‘parent’ plug-in) and we we want to create an add-on for it, B. We’ll allow the add on to be activated – but if A isn’t activated, we’ll display an error message:

register_activation_hook(__FILE__,'B_add_on_plugin_activate'); 
function B_add_on_plugin_activate(){
     $parent= 'A/A.php';

    if( is_plugin_active($parent) ){
        $plugins = get_plugins();
        $plugin_data = $plugins[$parent];

        //The parent plug-in is installed - but is it the correct version?
        if( $plugin_data['Version'] < '1.5' ){
            update_option('pluginA_install_nag','wrongversion');
        }
    }else{
        update_option('pluginA_install_nag','needtoinstall');
    }
}

add_action('load-plugins.php','B_add_on_activate_check');
function my_add_on_activate_check(){
    if( get_option('pluginA_install_nag',false) ){
        delete_option('pluginA_install_nag');
        add_action('admin_notices', 'B_add_on_install_A_notice');
    }
}

function B_add_on_install_A_notice(){
    echo '<div class="updated"><p>
        This plug-in requires <strong>A</strong> (v1.5 or higher) to function correctly. Download <a href="">A</a>.
    </p></div>';
}

Alternatively you could redirect from the activation, and – in effect – abort it. But by doing so, you obviously cant display a message (since your add-on plug-in won’t be activated …). This is probably bad, because it gives the user no feedback.

Deactivating…

What if someone installs the add-on plugin B, and then deactivates the A. Suppose, in this scenario we’d like to deactivate our add on plugin too. We might hope that we can just use deactivate_plugins inside the deactivation hook for A… unfortunately this doesn’t work and inspecting the code reveals why. I talk about it in this article.

One thought on “How to check another plug-in exists and get its details

  1. Deactivate Other Plug-ins On Deactivation | stephenharris.info

Leave a Reply

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