Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine if a WordPress plugin is just installed?

I like to know if there is any way to know if a plugin is Just installed. I don't care if that is activated yet, but only if that plugin is installed !

Do you know any good way to do that?

I have to be more specific. I know where to find the plugins and I know how I can see if there are installed. The question is, if there is any programaticly way to check if the plugin is installed.

ie: WordPress provide us with register_activation_hook() to make any operation we like on the plugin activation, but there is no any relevant hook for plugin installation. Is there any way to determine the plugin installation ?

like image 351
KodeFor.Me Avatar asked Oct 12 '11 10:10

KodeFor.Me


2 Answers

Here is the best way I have found so far: (this was written as of WP 3.5)

Info:

The main thing to keep in mind with register_activation_hook is that it is called as a mediary page between clicking an activate link from wp-admin/plugins.php and seeing the Plugin Activated notice after it has been hooked in. I found this out after I tried using the $_GET variable that are sent from the Activate link. If you take a good look the link you click too and the link you end up at are slightly different. This is why you cannot use add_action(), do_action() or add_filter(), .. within the register_activation_hook() triggers. It instantly redirects once a plugin has been activated.

Solution:

You can however use add_option during the hook processes like so

/* Main Plugin File */
...
register_activation_hook( __FILE__, function() {
  add_option('Activated_Plugin','Plugin-Slug');
  /* activation code here */
});

add_action('admin_init','load_plugin');
function load_plugin() {
    if(is_admin()&&get_option('Activated_Plugin')=='Plugin-Slug') {
     delete_option('Activated_Plugin');
     /* do some stuff once right after activation */
    }
}

That should get you started...

like image 72
Drone Brain Avatar answered Sep 20 '22 17:09

Drone Brain


There are several ways to check that I can think of:

  • Check that the plugin folder exists
  • Check that a hook or filter is defined
  • Check that a define() is defined - wait till plugins_loaded hook runs for this one
  • Check an options field for related values set by the plugin
  • Check that a class has been defined
like image 34
Brian C Avatar answered Sep 20 '22 17:09

Brian C