Using WordPress ‘get_broken_themes()’ PHP function

The get_broken_themes() WordPress PHP function retrieves a list of broken themes.

Usage

$broken_themes = get_broken_themes();

Parameters

  • None

More information

See WordPress Developer Resources: get_broken_themes()

Examples

Display Broken Themes

Display a list of broken themes with their respective errors.

$broken_themes = get_broken_themes();

if (!empty($broken_themes)) {
    echo "Broken Themes:\n";
    foreach ($broken_themes as $theme_name => $theme_data) {
        echo "- $theme_name: {$theme_data['Description']}\n";
    }
} else {
    echo "No broken themes found.";
}

Check if a Specific Theme is Broken

Check if a specific theme (e.g., “My Custom Theme”) is broken.

$theme_name = "My Custom Theme";
$broken_themes = get_broken_themes();

if (isset($broken_themes[$theme_name])) {
    echo "$theme_name is broken.";
} else {
    echo "$theme_name is not broken.";
}

Count Broken Themes

Display the number of broken themes.

$broken_themes = get_broken_themes();
echo "Number of broken themes: " . count($broken_themes);

Send Email Notification for Broken Themes

Send an email to the site administrator if there are broken themes.

$broken_themes = get_broken_themes();

if (!empty($broken_themes)) {
    $subject = "Broken themes detected on your WordPress site";
    $message = "The following themes are broken:\n\n";

    foreach ($broken_themes as $theme_name => $theme_data) {
        $message .= "- $theme_name: {$theme_data['Description']}\n";
    }

    wp_mail(get_option('admin_email'), $subject, $message);
}

Automatically Disable Broken Themes

Disable broken themes by setting a fallback theme (e.g., “Twenty Twenty-One”).

$fallback_theme = "Twenty Twenty-One";
$broken_themes = get_broken_themes();

if (!empty($broken_themes)) {
    foreach ($broken_themes as $theme_name => $theme_data) {
        if ($theme_name == wp_get_theme()->get('Name')) {
            switch_theme($fallback_theme);
            break;
        }
    }
}