Retrieving installed plugins in WordPress using get_plugins

The get_plugins function in WordPress is a part of the Plugin API. It is primarily used to retrieve details about all plugins that are currently installed in the WordPress installation, irrespective of whether they are active or inactive. The information returned by this function includes the plugin’s name, description, author, version number, and other metadata.

This function can be beneficial in several scenarios. For instance, it can be used to generate a list of all installed plugins for administrative purposes, or to check if a particular plugin is installed before running code that depends on that plugin.

It is important to note that the get_plugins function does not return any information about whether the plugins are currently active or not. To retrieve this information, other functions such as is_plugin_active need to be used.

Parameters Accepted by the get_plugins Function

The get_plugins function in WordPress accepts a specific parameter to facilitate its operation. This parameter is as follows:

  • $plugin_folder (string): This parameter, which is optional, represents the relative path to a specific plugin folder. By default, its value is an empty string (”).

Return Value of the get_plugins Function

The get_plugins function returns an array of plugin data. Each element in this array is another array containing data for a specific plugin. The key for each plugin data array is the name of the plugin file. The structure of the return value is based on the get_plugin_data() function.

Examples

How to Display All Installed Plugins

The following code snippet retrieves and displays a list of all installed plugins on your WordPress site.

<?php
// Verify the existence of the get_plugins() function. 
// This step is crucial on the website's front end, as it is typically
// included in a file that is usually loaded only within the admin area.
if ( ! function_exists( 'get_plugins' ) ) {
  require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
echo '<ul>';
foreach($all_plugins as $plugin => $plugin_data) {
 echo '<li>' . $plugin_data['Name'] . '</li>';
}
echo '</ul>';
?>

How to Check if a Specific Plugin is Installed

This code snippet checks if a specific plugin (in this case ‘Akismet’) is installed on your WordPress site.

<?php
if ( ! function_exists( 'get_plugins' ) ) {
  require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
if(isset($all_plugins['akismet/akismet.php'])) {
 echo "<p>Akismet is installed.</p>";
} else {
 echo "<p>Akismet is not installed.</p>";
}
?>

How to Count the Number of Installed Plugins

This code snippet retrieves the number of all installed plugins on your WordPress site.

<?php
if ( ! function_exists( 'get_plugins' ) ) {
  require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
echo "<p>Number of installed plugins: " . count($all_plugins) . "</p>";
?>

Conclusion

The get_plugins function serves as a key element in the WordPress platform, providing a detailed list of all the plugins that are currently installed, whether active or inactive. This function is primarily used for plugin management, enabling developers to obtain comprehensive information about the plugins on a site, which can be utilized for various purposes such as debugging, enhancing security, and optimizing performance. It is important to note that the get_plugins function does not distinguish between active and inactive plugins, it simply provides a complete list of all installed plugins.

Related WordPress Functions