register_uninstall_hook()
概述
register_uninstall_hook() 用于为插件设置卸载钩子,当用户点击卸载链接时触发回调函数执行清理操作。插件应避免在注册卸载钩子时运行任意代码,推荐使用 uninstall.php 文件来绕过钩子限制。
关键要点
- 注册卸载钩子后,插件卸载链接才会激活,回调函数必须是静态方法或函数。
- 插件不应在函数外运行代码,以免阻碍卸载过程;建议创建 uninstall.php 文件并检查 WP_UNINSTALL_PLUGIN 常量。
- 参数包括插件文件路径和回调函数,函数内部会检查并更新 uninstall_plugins 选项。
- 卸载钩子应在插件激活时注册一次,避免重复调用影响性能。
- 不能使用匿名函数作为回调,否则会导致序列化错误。
代码示例
function your_prefix_activate(){
register_uninstall_hook( __FILE__, 'your_prefix_uninstall' );
}
register_activation_hook( __FILE__, 'your_prefix_activate' );
function your_prefix_uninstall(){
// 卸载时执行的代码
}注意事项
- 确保 register_uninstall_hook() 只调用一次且值相同,防止性能问题。
- 避免在卸载钩子中使用非静态类方法或匿名函数。
Sets the uninstallation hook for a plugin.
Description
Registers the uninstall hook that will be called when the user clicks on the uninstall link that calls for the plugin to uninstall itself. The link won’t be active unless the plugin hooks into the action.
The plugin should not run arbitrary code outside of functions, when registering the uninstall hook. In order to run using the hook, the plugin will have to be included, which means that any code laying outside of a function will be run during the uninstallation process. The plugin should not hinder the uninstallation process.
If the plugin can not be written without running code within the plugin, then the plugin should create a file named ‘uninstall.php’ in the base plugin folder. This file will be called, if it exists, during the uninstallation process bypassing the uninstall hook. The plugin, when using the ‘uninstall.php’ should always check for the ‘WP_UNINSTALL_PLUGIN’ constant, before executing.
Parameters
$filestringrequired-
Plugin file.
$callbackcallablerequired-
The callback to run when the hook is called. Must be a static method or function.
Source
function register_uninstall_hook( $file, $callback ) {
if ( is_array( $callback ) && is_object( $callback[0] ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' );
return;
}
/*
* The option should not be autoloaded, because it is not needed in most
* cases. Emphasis should be put on using the 'uninstall.php' way of
* uninstalling the plugin.
*/
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
$plugin_basename = plugin_basename( $file );
if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) {
$uninstallable_plugins[ $plugin_basename ] = $callback;
update_option( 'uninstall_plugins', $uninstallable_plugins );
}
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 3 content
sangeethksin
Perform Uninstall hook inside register_activation_hook
function your_prefix_activate(){ register_uninstall_hook( __FILE__, 'your_prefix_uninstall' ); } register_activation_hook( __FILE__, 'your_prefix_activate' ); // And here goes the uninstallation function: function your_prefix_uninstall(){ // codes to perform during unistallation }register_uninstall_hook()writes an option each time it is called. This means your plugin would cause an option to be written on every regular page view of anonymous visitors on your website, which would be bad for performance. See https://core.trac.wordpress.org/ticket/31792 Also make sure that your plugin callsregister_uninstall_hook()only once and with the same value.Skip to note 4 content
Andrew Mead
Keep in mind that you cannot pass an anonymous function to
register_uninstall_hook. It will attempt to serialize the function resulting in the following error:PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in /redacted/wp-includes/functions.php:625There’s a issue about this here: https://core.trac.wordpress.org/ticket/31496