register_activation_hook

Set the activation hook for a plugin.

Description

register_activation_hook( (string) $file, (callable) $function ); 

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/plugins/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/plugins/sample.php the name of this hook will be 'activate_sample.php'.

Parameters (2)

0. $file (string)
The filename of the plugin including the path.
1. $function (callable)
The function hooked to the activate_PLUGIN action.

Usage

  1. if ( !function_exists( 'register_activation_hook' ) ) { 
  2. require_once ABSPATH . WPINC . '/plugin.php'; 
  3.  
  4. // The filename of the plugin including the path. 
  5. $file = ''; 
  6.  
  7. // The function hooked to the 'activate_PLUGIN' action. 
  8. $function = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = register_activation_hook($file, $function); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/plugin.php  
  1. function register_activation_hook($file, $function) { 
  2. $file = plugin_basename($file);