Languages: English • 日本語 (Add your language)
The register_activation_hook function registers a plugin function to be run when the plugin is activated.
When a plugin is activated, the action 'activate_PLUGINNAME' hook is called. In the name of this hook, PLUGINNAME is replaced with the name of the plugin, including the optional subdirectory. For example, when the plugin is located in wp-content/plugin/sampleplugin/sample.php, then the name of this hook will become 'activate_sampleplugin/sample.php'. When the plugin consists of only one file and is (as by default) located at wp-content/plugin/sample.php the name of this hook will be 'activate_sample.php'.
This function is a wrapper for the 'activate_PLUGINNAME' action, and is easier to use.
<?php register_activation_hook( $file, $function ); ?>
If you have a function called myplugin_activate() in the main plugin file at either
use this code:
function myplugin_activate() { // Activation code here... } register_activation_hook( __FILE__, 'myplugin_activate' );
This will call the myplugin_activate() function on activation of the plugin.
If your plugin uses the singleton class pattern, add the activation hook like so:
class MyPlugin { static function install() { // do not generate any output here } } register_activation_hook( __FILE__, array( 'MyPlugin', 'install' ) );
If the class that holds your activation function/method is in some additional file, register your activation function like this:
include_once dirname( __FILE__ ) . '/your_additional_file.php'; register_activation_hook( __FILE__, array( 'YourAdditionalClass', 'on_activate_function' ) );
Or, because the activation hook requires a static function, if you're inside of a __construct():
register_activation_hook( __FILE__, array( 'MyPlugin', 'YOUR_METHOD_NAME' ) );
If you are interested in doing something just after a plugin has been activated it is important to note that the hook process performs an instant redirect after it fires. So it is impossible to use add_action() or add_filter() type calls until the redirect has occurred (e.g., only two hooks are fired after the plugin's activation hook: 'activated_plugin' and 'shutdown'). A quick workaround to this quirk is to use add_option() like so:
/* Main Plugin File */ ... function my_plugin_activate() { add_option( 'Activated_Plugin', 'Plugin-Slug' ); /* activation code here */ } register_activation_hook( __FILE__, 'my_plugin_activate' ); function load_plugin() { if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) { delete_option( 'Activated_Plugin' ); /* do stuff once right after activation */ // example: add_action( 'init', 'my_init_function' ); } } add_action( 'admin_init', 'load_plugin' );
You can check out the full post @ http://stackoverflow.com/questions/7738953/is-there-a-way-to-determine-if-a-wordpress-plugin-is-just-installed/13927297#13927297.
However, it is possible to use do_action(), like this:
function my_plugin_activate() { do_action( 'my_plugin_activate' ); } register_activation_hook( __FILE__, 'my_plugin_activate' );
Included plugin files and even other plugins will be able to hook into this action.
If you're using global variables, you may find that the function you pass to register_activation_hook() does not have access to global variables at the point when it is called, even though you state their global scope within the function like this:
$myvar = 'whatever'; function myplugin_activate() { global $myvar; echo $myvar; // this will NOT be 'whatever'! } register_activation_hook( __FILE__, 'myplugin_activate' );
This is because on that very first include, your plugin is NOT included within the global scope. It's included in the activate_plugin() function, and so its "main body" is not automatically in the global scope.
This is why you should always be explicit. If you want a variable to be global, then you need to declare it as such, and that means anywhere and everywhere you use it. If you use it in the main body of the plugin, then you need to declare it global there too.
When activation occurs, your plugin is included from another function and then your myplugin_activate() is called from within that function (specifically, within the activate_plugin() function) at the point where your plugin is activated. The main body variables are therefore in the scope of the activate_plugin() function and are not global, unless you explicitly declare their global scope:
global $myvar; $myvar = 'whatever'; function myplugin_activate() { global $myvar; echo $myvar; // this will be 'whatever' } register_activation_hook( __FILE__, 'myplugin_activate' );
More information on this is available here: http://wordpress.org/support/topic/201309
register_activation_hook() is located in wp-includes/plugin.php