register_uninstall_hook

函数
register_uninstall_hook ( $file, $callback )
参数
  • (string) $file Plugin file.
    Required:
  • (callable) $callback The callback to run when the hook is called. Must be a static method or function.
    Required:
定义位置
相关方法
register_taxonomyregister_activation_hookregister_initial_settingsunregister_taxonomyunregister_meta_key
引入
2.7.0
弃用
-

register_uninstall_hook: 这个函数允许你注册一个函数,当插件被卸载时将被调用: 当你需要清理你的插件在安装过程中创建的任何数据时,这很有用。

设置插件的卸载钩子。

注册卸载钩子,当用户点击卸载链接时,该钩子将被调用,该链接要求该插件自行卸载。该链接将不会 该链接不会被激活,除非该插件钩住了该动作。

在注册卸载钩子时,该插件不应运行函数之外的任意代码。为了使用该钩子运行,该插件
将必须被包含在内,这意味着在卸载过程中,任何铺设在函数之外的代码都将被运行。该插件不应妨碍卸载过程。

如果不能在不运行插件内的代码的情况下编写插件,那么插件应该在基本插件文件夹中创建一个名为’uninstall.php’的文件。这个文件将被调用,如果它存在的话,在卸载过程中绕过卸载钩。插件在使用”uninstall.php”时,应该在执行前检查”WP_UNINSTALL_PLUGIN”常量。

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 );
	}
}

常见问题

FAQs
查看更多 >