WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/register activation hook

Description

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.

Usage

 <?php register_activation_hook$file$function ); ?> 

Parameters

$file
(string) (required) Path to the main plugin file inside the wp-content/plugins directory. A full path will work.
Default: None
$function
(callback) (required) The function to be run when the plugin is activated. Any of PHP's callback pseudo-types will work.
Default: None

Examples

If you have a function called myplugin_activate() in the main plugin file at either

  • wp-content/plugins/myplugin.php or
  • wp-content/plugins/myplugin/myplugin.php

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' ) );

Notes

  • Related discussion with another sample of working code: http://wordpress.org/support/topic/312342
  • Registering the hook inside the 'plugins_loaded' hook will not work. You can't call register_activation_hook() inside a function hooked to the 'plugins_loaded' or 'init' hooks (or any other hook). These hooks are called before the plugin is loaded or activated.
  • When a plugin is activated, all active plugins are loaded, then the plugin being activated. The plugin's activation hook is run and then the page is immediately redirected (see below).

Process Flow

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.

A Note on Variable Scope

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

Discussions - External Resources

Changelog

Source File

register_activation_hook() is located in wp-includes/plugin.php

Related

See also index of Function Reference and index of Template Tags.