函数原型
register_activation_hook( string $file, callable $callback )
函数描述
Set the activation hook for a plugin.
是否弃用
未弃用
函数参数
-
$file
string
Required - The filename of the plugin including the path.
-
$callback
callable
Required - The function hooked to the
'activate_PLUGIN'action.
函数返回值
无
函数位置
File: wp-includes/plugin.php.
函数源码
/* 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' );
源码链接
变更日志
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |

