Codex

Function Reference/register activation hook

The function register_activation_hook (introduced in WordPress 2.0) registers a plugin function to be run when the plugin is activated.

This is easier than using the activate_pluginname action.

Usage and parameters

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

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

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:

register_activation_hook( __FILE__, 'myplugin_activate' );

This will call the myplugin_activate() function on activation of the plugin. This is a more reliable method than using the activate_pluginname action.

See also register_deactivation_hook