函数文档

is_uninstallable_plugin()

💡 云策文档标注

概述

is_uninstallable_plugin() 函数用于判断指定插件是否可被卸载,通过检查插件是否在卸载选项列表中或存在 uninstall.php 文件来实现。

关键要点

  • 函数接受一个必需参数 $plugin,表示插件文件相对于插件目录的路径。
  • 返回布尔值,true 表示插件可卸载,false 表示不可卸载。
  • 检查逻辑包括:验证插件是否在 get_option('uninstall_plugins') 返回的数组中,或插件目录下是否存在 uninstall.php 文件。

代码示例

function is_uninstallable_plugin( $plugin ) {
	$file = plugin_basename( $plugin );

	$uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
	if ( isset( $uninstallable_plugins[ $file ] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) {
		return true;
	}

	return false;
}

注意事项

  • 此函数自 WordPress 2.7.0 版本引入。
  • 相关函数包括 plugin_basename() 和 get_option(),用于获取插件基本名称和选项值。
  • 被 WP_Plugins_List_Table::single_row() 和 delete_plugins() 等函数调用,用于插件列表显示和删除操作。

📄 原文内容

Determines whether the plugin can be uninstalled.

Parameters

$pluginstringrequired
Path to the plugin file relative to the plugins directory.

Return

bool Whether plugin can be uninstalled.

Source

function is_uninstallable_plugin( $plugin ) {
	$file = plugin_basename( $plugin );

	$uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
	if ( isset( $uninstallable_plugins[ $file ] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) {
		return true;
	}

	return false;
}

Changelog

Version Description
2.7.0 Introduced.