Using WordPress ‘get_plugin_updates()’ PHP function

The get_plugin_updates() WordPress PHP function retrieves a list of plugins with updates available.

Usage

$updates = get_plugin_updates();

Parameters

  • None

More information

See WordPress Developer Resources: get_plugin_updates()

Examples

Displaying a list of plugins with updates available

This code snippet will display a list of plugin names with updates available:

$plugins_with_updates = get_plugin_updates();
foreach ($plugins_with_updates as $plugin) {
    echo $plugin->Name . '<br>';
}

Checking for a specific plugin update

This code snippet checks if a specific plugin, identified by its path, has an update available:

$plugin_path = 'my-plugin/my-plugin.php';
$plugins_with_updates = get_plugin_updates();

if (array_key_exists($plugin_path, $plugins_with_updates)) {
    echo 'Update available for My Plugin';
} else {
    echo 'My Plugin is up-to-date';
}

Displaying plugin update details

This code snippet displays the update details for each plugin with an update available:

$plugins_with_updates = get_plugin_updates();
foreach ($plugins_with_updates as $plugin_path => $plugin) {
    echo 'Plugin Name: ' . $plugin->Name . '<br>';
    echo 'Current Version: ' . $plugin->Version . '<br>';
    echo 'New Version: ' . $plugin->update->new_version . '<br><br>';
}

Counting plugins with updates

This code snippet counts the number of plugins with updates available:

$plugins_with_updates = get_plugin_updates();
$update_count = count($plugins_with_updates);
echo 'There are ' . $update_count . ' plugin updates available';

This code snippet displays plugin names with a link to their update page in the admin area:

$plugins_with_updates = get_plugin_updates();
foreach ($plugins_with_updates as $plugin_path => $plugin) {
    $update_url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . urlencode($plugin_path)), 'upgrade-plugin_' . $plugin_path);
    echo '<a href="' . $update_url . '">' . $plugin->Name . '</a><br>';
}