Problem Analysis
To get information about installed plugins, which provides plugin’s version, we’ll use get_plugins()
function. Keep in mind, that this function is available in back end only, so our code must reside there. Data, returned from this function, does not tell us, which plugins are active. To filter out active plugins, we’ll use get_option('active_plugins')
function. It will return a simple array, consisting of keys to array returned by get_plugins()
function.
Code
The best illustration for our method will be the actual code. We’ll create a dashboard widget, displaying three-column table, showing active plugin’s name, its current version, and its newest version in WordPress repository. Let’s create myDashboardWidget.php
file, and put the code below, in it.
<?php
if(is_admin()) { // make sure, the following code runs in back end
// returns version of the plugin represented by $slug, from repository
function getPluginVersionFromRepository($slug) {
$url = "https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&request[slugs][]={$slug}";
$response = wp_remote_get($url); // WPOrg API call
$plugins = json_decode($response['body']);
// traverse $response object
foreach($plugins as $key => $plugin) {
$version = $plugin->version;
}
return $version;
}
// dashboard widget's callback
function activePluginsVersions() {
$allPlugins = get_plugins(); // associative array of all installed plugins
$activePlugins = get_option('active_plugins'); // simple array of active plugins
// building active plugins table
echo '<table width="100%">';
echo '<thead>';
echo '<tr>';
echo '<th width="50%" style="text-align:left">Plugin</th>';
echo '<th width="20%" style="text-align:left">currVer</th>';
echo '<th width="20%" style="text-align:left">repoVer</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
// traversing $allPlugins array
foreach($allPlugins as $key => $value) {
if(in_array($key, $activePlugins)) { // display active only
echo '<tr>';
echo "<td>{$value['Name']}</td>";
echo "<td>{$value['Version']}</td>";
$slug = explode("https://wordpress.stackexchange.com/",$key)[0]; // get active plugin's slug
// get newest version of active plugin from repository
$repoVersion = getPluginVersionFromRepository($slug);
echo "<td>{$repoVersion}</td>";
echo '</tr>';
}
}
echo '</tbody>';
echo '</table>';
}
// widget's registration
function fpwAddDashboardWidget() {
wp_add_dashboard_widget(
'active_plugins_versions', // widget's ID
'Active Plugins Versions', // widget's title
'activePluginsVersions' // widget's callback (content)
);
}
add_action('wp_dashboard_setup', 'fpwAddDashboardWidget');
}
Now, place this file in /wp-content/mu-plugins/
directory.
Code Explained
Read comments within myDashboardWidget.php
file.
Note: if there is no version shown in third column of the table, the plugin is not in repository (either premium, or discontinued).
Code Efficiency
Efficiency of this sample code can be much improved. The WPOrg API request is made for each plugin. /plugins/info/1.2/
version of the API, allows getting info for all plugin slugs with one request only.