Skip to content

Instantly share code, notes, and snippets.

@MogulChris
Created March 4, 2019 21:35
Show Gist options
  • Save MogulChris/d221339a8dcf0581b20c58894f02ca45 to your computer and use it in GitHub Desktop.
Save MogulChris/d221339a8dcf0581b20c58894f02ca45 to your computer and use it in GitHub Desktop.
Get a list of active Wordpress plugins, current versions and available versions
<?php
//based on https://wordpress.stackexchange.com/a/298323/88160 but standalone
require_once('wp-load.php');
ini_set("memory_limit","512M");
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
// 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) {
if(!empty($plugin->version)){
$version = $plugin->version;
}
else{
$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('/',$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>';
}
activePluginsVersions();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment