卸载单个插件。

原型

uninstall_plugin( string $plugin )

描述

调用卸载钩子(如果可用)。

参数

$plugin (string) (Required) 插件目录中主插件文件的路径。

返回值

(true)  如果已找到并包含插件的uninstall.php文件,则为True。

源文件

路径:wp-admin/includes/plugin.php

<?php
...
function uninstall_plugin($plugin) {
	$file = plugin_basename($plugin);
	$uninstallable_plugins = (array) get_option('uninstall_plugins');
	/**
	 * Fires in uninstall_plugin() immediately before the plugin is uninstalled.
	 *
	 * @since 4.5.0
	 *
	 * @param string $plugin                Path to the main plugin file from plugins directory.
	 * @param array  $uninstallable_plugins Uninstallable plugins.
	 */
	do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins );
	if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
		if ( isset( $uninstallable_plugins[$file] ) ) {
			unset($uninstallable_plugins[$file]);
			update_option('uninstall_plugins', $uninstallable_plugins);
		}
		unset($uninstallable_plugins);
		define('WP_UNINSTALL_PLUGIN', $file);
		wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
		include( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' );
		return true;
	}
	if ( isset( $uninstallable_plugins[$file] ) ) {
		$callable = $uninstallable_plugins[$file];
		unset($uninstallable_plugins[$file]);
		update_option('uninstall_plugins', $uninstallable_plugins);
		unset($uninstallable_plugins);
		wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
		include( WP_PLUGIN_DIR . '/' . $file );
		add_action( "uninstall_{$file}", $callable );
		/**
		 * Fires in uninstall_plugin() once the plugin has been uninstalled.
		 *
		 * The action concatenates the 'uninstall_' prefix with the basename of the
		 * plugin passed to uninstall_plugin() to create a dynamically-named action.
		 *
		 * @since 2.7.0
		 */
		do_action( "uninstall_{$file}" );
	}
}
...
?>

其他

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