设置插件的卸载挂钩。

原型

register_uninstall_hook( string $file, callable $callback )

描述

注册当用户单击要求插件自行卸载的卸载链接时将调用的卸载挂钩。除非插件挂钩到操作中,否则链接将不会处于活动状态。

参数

$file (string) (Required) 插件文件。

$callback (callable) (Required) 调用钩子时运行的回调。必须是静态方法或函数。

源文件

路径:wp-includes/plugin.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
...
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');
	$uninstallable_plugins[plugin_basename($file)] = $callback;

	update_option('uninstall_plugins', $uninstallable_plugins);
}

...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/register_uninstall_hook/