Using WordPress ‘is_uninstallable_plugin()’ PHP function

The is_uninstallable_plugin() WordPress PHP function determines whether a plugin can be uninstalled.

Usage

To use the is_uninstallable_plugin() function, pass the plugin file path relative to the plugins directory as a string:

is_uninstallable_plugin($plugin);

Parameters

  • $plugin (string) – Required: Path to the plugin file relative to the plugins directory.

More information

See WordPress Developer Resources: is_uninstallable_plugin()

Examples

Check if a plugin can be uninstalled

This example checks if the plugin my-plugin/my-plugin.php can be uninstalled.

$plugin = 'my-plugin/my-plugin.php';
$uninstallable = is_uninstallable_plugin($plugin);

if ($uninstallable) {
    echo "The plugin can be uninstalled.";
} else {
    echo "The plugin cannot be uninstalled.";
}

Display uninstallable plugins from an array

This example displays all uninstallable plugins from an array of plugins.

$plugins = [
    'plugin1/plugin1.php',
    'plugin2/plugin2.php',
    'plugin3/plugin3.php'
];

foreach ($plugins as $plugin) {
    if (is_uninstallable_plugin($plugin)) {
        echo $plugin . " can be uninstalled.<br>";
    }
}

Get uninstallable plugins from all active plugins

This example retrieves all uninstallable plugins from the list of active plugins.

$active_plugins = get_option('active_plugins');
$uninstallable_plugins = [];

foreach ($active_plugins as $plugin) {
    if (is_uninstallable_plugin($plugin)) {
        $uninstallable_plugins[] = $plugin;
    }
}

print_r($uninstallable_plugins);

Create a function to check if a plugin is uninstallable

This example creates a reusable function check_plugin_uninstallable that checks if a given plugin is uninstallable.

function check_plugin_uninstallable($plugin) {
    if (is_uninstallable_plugin($plugin)) {
        return true;
    } else {
        return false;
    }
}

$plugin = 'my-plugin/my-plugin.php';
$result = check_plugin_uninstallable($plugin);

if ($result) {
    echo "The plugin can be uninstalled.";
} else {
    echo "The plugin cannot be uninstalled.";
}

Uninstall all uninstallable plugins

This example uninstalls all uninstallable plugins from the list of active plugins.

$active_plugins = get_option('active_plugins');

foreach ($active_plugins as $plugin) {
    if (is_uninstallable_plugin($plugin)) {
        uninstall_plugin($plugin);
        echo $plugin . " has been uninstalled.<br>";
    }
}