The display_theme() WordPress PHP function is used to print a theme on the Install Themes pages.
Usage
The function uses a theme object as a parameter. Here’s a basic example:
$theme = wp_get_theme(); // Get current theme display_theme($theme); // Display current theme
Parameters
- $theme (WP_Theme object, Required): The theme object you want to display. You can get it using functions like wp_get_theme().
More information
See WordPress Developer Resources: display_theme()
The function is part of the WordPress core and has been available since version 2.9.0. It is typically used in WordPress admin theme install pages.
Examples
Displaying Current Theme
In this example, we get the current theme and display it using display_theme().
$theme = wp_get_theme(); // Get current theme display_theme($theme); // Display current theme
Displaying a Specific Theme
In this example, we display a specific theme by name using wp_get_theme() and display_theme().
$theme = wp_get_theme('twentytwentyone'); // Get specific theme display_theme($theme); // Display the specific theme
Checking if a Theme Exists Before Displaying
In this example, we first check if a theme exists before trying to display it.
$theme_name = 'twentytwentyone'; if (wp_get_theme($theme_name)->exists()) { // Check if theme exists $theme = wp_get_theme($theme_name); // Get the theme display_theme($theme); // Display the theme }
Displaying a Theme and Its Details
In this example, we display a theme and its details like Theme Name and Version.
$theme = wp_get_theme('twentytwentyone'); // Get specific theme display_theme($theme); // Display the theme echo 'Theme Name: ' . $theme->get('Name'); // Display Theme Name echo 'Version: ' . $theme->get('Version'); // Display Theme Version
Switching Themes and Displaying the New Theme
In this example, we switch to a new theme and then display it.
$theme_name = 'twentytwentyone'; switch_theme($theme_name); // Switch to a new theme $theme = wp_get_theme(); // Get current theme which is the new theme now display_theme($theme); // Display the new theme