Using WordPress ‘install_plugin_install_status()’ PHP function

The install_plugin_install_status() WordPress PHP function determines the status we can perform on a plugin.

Usage

Here’s a generic example of how to use the function:

$status = install_plugin_install_status($api, $loop);

Parameters

  • $api (array|object) – Required. Data about the plugin retrieved from the API.
  • $loop (bool) – Optional. Disable further loops. Default: false.

More information

See WordPress Developer Resources: install_plugin_install_status()

Examples

Check if a plugin is installed

In this example, we check if a plugin is already installed by using the install_plugin_install_status() function.

$api = plugins_api('plugin_information', array('slug' => 'plugin-slug'));
$status = install_plugin_install_status($api);

if ($status['status'] == 'install') {
    echo 'Plugin not installed';
} else {
    echo 'Plugin installed';
}

Display plugin installation status

This example demonstrates how to display the installation status of a plugin using install_plugin_install_status().

$api = plugins_api('plugin_information', array('slug' => 'plugin-slug'));
$status = install_plugin_install_status($api);

echo 'Plugin installation status: ' . $status['status'];

Prevent plugin installation if already installed

In this example, we prevent the installation of a plugin if it’s already installed by utilizing the install_plugin_install_status() function.

$api = plugins_api('plugin_information', array('slug' => 'plugin-slug'));
$status = install_plugin_install_status($api);

if ($status['status'] != 'install') {
    echo 'Plugin is already installed. Aborting installation.';
} else {
    // Install plugin
}

Get the plugin installation status without further loops

This example shows how to get the plugin installation status without further loops using install_plugin_install_status().

$api = plugins_api('plugin_information', array('slug' => 'plugin-slug'));
$status = install_plugin_install_status($api, true);

echo 'Plugin installation status: ' . $status['status'];

Get the plugin version from the installation status

In this example, we get the plugin version from the installation status using the install_plugin_install_status() function.

$api = plugins_api('plugin_information', array('slug' => 'plugin-slug'));
$status = install_plugin_install_status($api);

echo 'Plugin version: ' . $status['version'];