Using WordPress ‘get_theme_updates()’ PHP function

The get_theme_updates() WordPress PHP function retrieves a list of themes that have updates available.

Usage

$themes_with_updates = get_theme_updates();

Parameters

  • None

More information

See WordPress Developer Resources: get_theme_updates()

Examples

Display a list of themes with updates available

This example retrieves themes with updates and displays their names and version numbers.

$themes_with_updates = get_theme_updates();

foreach ($themes_with_updates as $theme) {
    echo 'Theme: ' . $theme->get('Name') . ', Version: ' . $theme->get('Version') . '<br>';
}

Update all themes with available updates

This example updates all themes that have updates available.

require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/theme.php';

$themes_with_updates = get_theme_updates();

foreach ($themes_with_updates as $theme) {
    $theme_upgrader = new Theme_Upgrader();
    $theme_upgrader->upgrade($theme->get_stylesheet());
}

Count the number of themes with updates available

This example counts the number of themes that have updates available.

$themes_with_updates = get_theme_updates();
$count = count($themes_with_updates);

echo 'Number of themes with updates: ' . $count;

Send an email when themes have updates available

This example sends an email to the site administrator if there are themes with updates available.

$themes_with_updates = get_theme_updates();

if (count($themes_with_updates) > 0) {
    $to = get_option('admin_email');
    $subject = 'Themes with updates available';
    $message = 'There are themes with updates available on your WordPress site.';
    wp_mail($to, $subject, $message);
}

Display a notice in the admin dashboard if themes have updates available

This example displays a notice in the admin dashboard if there are themes with updates available.

add_action('admin_notices', 'show_theme_update_notice');

function show_theme_update_notice() {
    $themes_with_updates = get_theme_updates();

    if (count($themes_with_updates) > 0) {
        echo '<div class="notice notice-warning"><p>There are themes with updates available. Please check the themes page.</p></div>';
    }
}