插件开发文档

激活/停用钩子

💡 云策文档标注

概述

激活和停用钩子允许插件在激活或停用时执行特定操作,如设置默认值或清理临时数据。这些钩子通过 register_activation_hook() 和 register_deactivation_hook() 函数实现,常用于处理自定义文章类型和重写规则。

关键要点

  • 激活钩子用于插件激活时执行任务,如添加重写规则、数据库表或设置默认选项值。
  • 停用钩子用于插件停用时执行任务,如清理缓存、临时文件等临时数据。
  • 停用钩子与卸载钩子不同:卸载钩子用于永久删除所有数据(如插件选项和自定义表)。
  • 使用 register_activation_hook() 和 register_deactivation_hook() 时,第一个参数必须指向主插件文件(包含插件头注释的文件)。

代码示例

// 激活钩子示例:注册自定义文章类型并刷新重写规则
function pluginprefix_activate() {
    pluginprefix_setup_post_type();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'pluginprefix_activate' );

// 停用钩子示例:注销自定义文章类型并刷新重写规则
function pluginprefix_deactivate() {
    unregister_post_type( 'book' );
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );

注意事项

  • 确保 register_activation_hook() 和 register_deactivation_hook() 的第一个参数正确指向主插件文件,否则钩子可能无法触发。
  • 激活钩子常用于刷新重写规则以避免 404 错误,特别是在注册自定义文章类型时。
  • 停用钩子不应用于永久删除数据;这应通过卸载钩子处理。

📄 原文内容

Activation and deactivation hooks provide ways to perform actions when plugins are activated or deactivated.

  • On activation, plugins can run a routine to add rewrite rules, add custom database tables, or set default option values.
  • On deactivation, plugins can run a routine to remove temporary data such as cache and temp files and directories.
The deactivation hook is sometimes confused with the uninstall hook. The uninstall hook is best suited to delete all data permanently such as deleting plugin options and custom tables, etc.

Activation

To set up an activation hook, use the register_activation_hook() function:

register_activation_hook(
	__FILE__,
	'pluginprefix_function_to_run'
);

Deactivation

To set up a deactivation hook, use the register_deactivation_hook() function:

register_deactivation_hook(
	__FILE__,
	'pluginprefix_function_to_run'
);

The first parameter in each of these functions refers to your main plugin file, which is the file in which you have placed the plugin header comment. Usually these two functions will be triggered from within the main plugin file; however, if the functions are placed in any other file, you must update the first parameter to correctly point to the main plugin file.

Example

One of the most common uses for an activation hook is to refresh WordPress permalinks when a plugin registers a custom post type. This gets rid of the nasty 404 errors.

Let’s look at an example of how to do this:

/**
 * Register the "book" custom post type
 */
function pluginprefix_setup_post_type() {
	register_post_type( 'book', ['public' => true ] ); 
} 
add_action( 'init', 'pluginprefix_setup_post_type' );


/**
 * Activate the plugin.
 */
function pluginprefix_activate() { 
	// Trigger our function that registers the custom post type plugin.
	pluginprefix_setup_post_type(); 
	// Clear the permalinks after the post type has been registered.
	flush_rewrite_rules(); 
}
register_activation_hook( __FILE__, 'pluginprefix_activate' );

If you are unfamiliar with registering custom post types, don’t worry – this will be covered later. This example is used simply because it’s very common.

Using the example from above, the following is how to reverse this process and deactivate a plugin:

/**
 * Deactivation hook.
 */
function pluginprefix_deactivate() {
	// Unregister the post type, so the rules are no longer in memory.
	unregister_post_type( 'book' );
	// Clear the permalinks to remove our post type's rules from the database.
	flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );

For further information regarding activation and deactivation hooks, here are some excellent resources: