register_activation_hook()注册一个插件激活钩子

目录

描述

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.

register_activation_hook() 描述

用法

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

register_activation_hook() 用法

参数

$file

(string) (必填) Path to the main plugin file inside the wp-content/plugins directory. A full path will work.

默认值: None

$function

(callback) (必填) The function to be run when the plugin is activated. Any of PHP's callback pseudo-types will work.

默认值: None

register_activation_hook() 参数

示例

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:

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:

If the class that holds your activation function/method is in some additional file, register your activation function like this:

Or, because the activation hook requires a static function, if you're inside of a __construct():

register_activation_hook() 示例

注意

  • 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:

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:

Included plugin files and even other plugins will be able to hook into this 动作.

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:

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:

More information on this is available here: http://wordpress.org/support/topic/201309

register_activation_hook() 注意

源文件

register_activation_hook() 函数的代码位于 wp-includes/plugin.php

register_activation_hook() 源文件

相关