The get_allowed_themes() WordPress PHP function retrieves the allowed themes for the current site.
Usage
$allowed_themes = get_allowed_themes();
Parameters
- None
More information
See WordPress Developer Resources: get_allowed_themes()
Examples
Display a list of allowed themes
This code retrieves a list of allowed themes and displays their names.
$allowed_themes = get_allowed_themes();
foreach ($allowed_themes as $theme) {
echo $theme->get('Name') . '<br>';
}
Check if a specific theme is allowed
This code checks if the “Twenty Twenty-One” theme is allowed on the current site.
$allowed_themes = get_allowed_themes();
if (isset($allowed_themes['twentytwentyone'])) {
echo 'The Twenty Twenty-One theme is allowed.';
} else {
echo 'The Twenty Twenty-One theme is not allowed.';
}
Count the number of allowed themes
This code counts the number of allowed themes and displays the result.
$allowed_themes = get_allowed_themes(); $allowed_themes_count = count($allowed_themes); echo 'There are ' . $allowed_themes_count . ' allowed themes.';
Display allowed themes as a dropdown list
This code creates a dropdown list with the allowed themes.
<select name="theme_selection">
<?php
$allowed_themes = get_allowed_themes();
foreach ($allowed_themes as $theme) {
$theme_name = $theme->get('Name');
echo '<option value="' . $theme_name . '">' . $theme_name . '</option>';
}
?>
</select>
Apply a random allowed theme
This code applies a random allowed theme to the current site.
$allowed_themes = get_allowed_themes(); $random_theme_key = array_rand($allowed_themes); $random_theme = $allowed_themes[$random_theme_key]; switch_theme($random_theme->get_stylesheet());