wp_get_theme( string $stylesheet = '', string $theme_root = '' ): WP_Theme
- Since
- 3.4.0
- Source
wp-includes/theme.php:117
Instantiates a WP_Theme object for the given theme stylesheet, or for the active theme when no argument is passed. The object is created even when the theme folder does not exist, so code must call the returned object's exists() method before trusting its data. Use $theme_root only when the theme lives somewhere get_raw_theme_root() would not find on its own, such as a directory registered with register_theme_directory().
Compatibility
- WordPress
- since 3.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$stylesheetstringoptional- Directory name for the theme. Defaults to active theme.Default:
'' $theme_rootstringoptional- Absolute path of the theme root to look in.
If not specified, get_raw_theme_root() is used to calculate the theme root for the $stylesheet provided (or active theme).Default:''
Return value
WP_Theme- Theme object. Be sure to check the object's exists() method if you need to confirm the theme's existence.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Get the name and version of the currently active theme
Calling wp_get_theme() with no arguments returns a WP_Theme object for whatever theme is active on the site.
$theme = wp_get_theme();
printf(
'Active theme: %s (version %s)',
esc_html( $theme->get( 'Name' ) ),
esc_html( $theme->get( 'Version' ) )
);Check whether a theme stylesheet exists before doing anything with it
Because wp_get_theme() always returns a WP_Theme object, guard code that switches or configures a theme with an exists() check first.
$stylesheet = 'not-a-real-theme-slug';
$theme = wp_get_theme( $stylesheet );
if ( $theme->exists() ) {
echo esc_html( $theme->get( 'Name' ) ) . ' is installed.';
} else {
echo 'No theme found with the stylesheet "' . esc_html( $stylesheet ) . '".';
}exists() is the only reliable way to tell a real theme from a made-up stylesheet, since the constructor never returns false.
Common problems and fixes · 3
- Why does wp_get_theme() with a fake stylesheet name not return false?
- Why did my custom theme directory get ignored when I called wp_get_theme()?
- Why does calling wp_get_theme() with no arguments give me the parent theme instead of the child theme I expected, or vice versa?
Why does wp_get_theme() with a fake stylesheet name not return false?
return new WP_Theme( $stylesheet, $theme_root ), so it hands back an object no matter what you pass in. There is no built-in falsey signal for a missing theme.
- Call $theme->exists() and branch on that.
- Do not use wp_get_theme( $slug ) in an if() check by itself; it is always truthy.Why did my custom theme directory get ignored when I called wp_get_theme()?
Why does calling wp_get_theme() with no arguments give me the parent theme instead of the child theme I expected, or vice versa?
Alternatives and related functions
wp_get_themes- When you need every installed theme rather than one specific stylesheet, since it returns an array of WP_Theme objects keyed by stylesheet.
get_stylesheet- When you only need the active theme's directory name as a plain string, not a full WP_Theme object.
get_template- When you specifically need the parent theme's stylesheet in a child theme setup, rather than the active child theme.
switch_theme- When the goal is to activate a theme rather than just read its data.
Performance profile
How much work a call to wp_get_theme() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 12–28
- Plugin surface
- None
- Called by
- 50
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 31.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_get_theme() - optionoption read or write
get_option()one call below wp_get_theme()
Further down the call graph this can also reach cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_theme() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($stylesheet) && !empty($theme_root) | 12 | none |
empty($stylesheet) && !empty($theme_root) | 15 | get_stylesheet() |
!empty($stylesheet) && empty($theme_root) | 21–25 | get_raw_theme_root() |
empty($stylesheet) && empty($theme_root) | 24–28 | get_stylesheet(), get_raw_theme_root() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 31 | 12–28 | 4 | |
| 8.5 | 31 | 12–28 | 4 | |
| 8.4 | 31 | 12–28 | 4 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 34 | 12–31 | 4 | |
| 8.2 | 34 | 12–31 | 4 | |
| 8.1 | 34 | 12–31 | 4 | |
| 7.4 | 34 | 12–31 | 4 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- get_stylesheet()Retrieves name of the current stylesheet.
- get_raw_theme_root()Gets the raw theme root relative to the content directory with no filters applied.
- WP_Theme::__construct()Constructor for WP_Theme.
Used by · 50
- Language_Pack_Upgrader::get_name_for_update()Gets the name of an item being updated.
- Theme_Upgrader::theme_info()Gets the WP_Theme object for a theme.
- Twenty_Twenty_One_Custom_Colors::editor_custom_color_variables()Editor custom color variables.
- Twenty_Twenty_One_Customize_Color_Control::enqueue()Enqueues control related scripts/styles.
- Twenty_Twenty_One_Dark_Mode::customize_controls_enqueue_scripts()Enqueues scripts for the customizer.
- Twenty_Twenty_One_Dark_Mode::editor_custom_color_variables()Enqueues editor custom color variables & scripts.
- Twenty_Twenty_One_Dark_Mode::enqueue_scripts()Enqueues scripts and styles.
- WP_Automatic_Updater::update()Updates an item, if appropriate.
- WP_Customize_Manager::__construct()Constructor.
- WP_Customize_Manager::theme()Gets the theme being customized.
- WP_Debug_Data::get_wp_active_theme()Gets the WordPress active theme section of the debug data.
- WP_Debug_Data::get_wp_parent_theme()Gets the WordPress parent theme section of the debug data.
Show all 50
- WP_Debug_Data::get_wp_themes_inactive()Gets the WordPress inactive themes section of the debug data.
- WP_REST_Posts_Controller::check_template()Checks whether the template is valid for the given post.
- WP_REST_Posts_Controller::handle_template()Sets the template for a post.
- WP_REST_Server::add_active_theme_link_to_index()Adds a link to the active theme for users who have proper permissions.
- WP_REST_Templates_Controller::get_wp_templates_author_text_field()Returns a human readable text for the author of the template.
- WP_REST_Themes_Controller::get_item()Retrieves a single theme.
- WP_REST_Themes_Controller::get_item_permissions_check()Checks if a given request has access to read the theme.
- WP_REST_Themes_Controller::get_items()Retrieves a collection of themes.
- WP_REST_Themes_Controller::prepare_item_for_response()Prepares a single theme output for response.
- WP_REST_Themes_Controller::prepare_links()Prepares links for the request.
- WP_Recovery_Mode_Email_Service::get_cause()Gets the description indicating the possible cause for the error.
- WP_Recovery_Mode_Email_Service::get_debug()Return debug information in an easy to manipulate format.
- WP_Site_Health::get_test_theme_version()Tests if themes are outdated, or unnecessary.
- WP_Theme::get_core_default_theme()Determines the latest WordPress default theme that is installed.
- WP_Theme_Install_List_Table::_get_theme_status()Checks to see if the theme is already installed.
- WP_Theme_JSON_Resolver::get_style_variations()Returns the style variations defined by the theme.
- WP_Theme_JSON_Resolver::get_theme_data()Returns the theme's data.
- WP_Theme_JSON_Resolver::get_user_data()Returns the user's origin config.
- WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles()Returns the custom post type that contains the user's origin config for the active theme or an empty array if none are found.
- WP_Theme_JSON_Resolver::get_user_global_styles_post_id()Returns the ID of the custom post type that stores user data.
- _register_theme_block_patterns()Register any patterns that the active theme may provide under its `./patterns/` directory.
- _wp_get_entity_view_config_posttype_wp_template()Provides the view configuration for the `wp_template` post type.
- check_theme_switched()Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
- current_theme_info()Retrieves information on the current active theme.
- delete_theme()Removes a theme.
- get_block_theme_folders()For backward compatibility reasons, block themes might be using block-templates or block-template-parts, this function ensures we fallback to these folders properly.
- get_current_theme()Retrieve current theme name.
- get_page_templates()Gets the page templates available in this theme.
- get_theme_mods()Retrieves all theme modifications.
- get_theme_updates()Retrieves themes with updates available.
- populate_network_meta()Creates WordPress network meta and sets the default values.
- populate_options()Create WordPress options and set the default values.
- remove_theme_mods()Removes theme modifications option for the active theme.
- switch_theme()Switches the theme.
- twenty_twenty_one_scripts()Enqueues scripts and styles.
- twentyeleven_theme_options_render_page()Renders the theme options page for Twenty Eleven.
- twentynineteen_scripts()Enqueues scripts and styles.
- twentytwenty_block_editor_styles()Enqueues supplemental block editor styles.
Source code
function wp_get_theme( $stylesheet = '', $theme_root = '' ) { global $wp_theme_directories; if ( empty( $stylesheet ) ) { $stylesheet = get_stylesheet(); } if ( empty( $theme_root ) ) { $theme_root = get_raw_theme_root( $stylesheet ); if ( false === $theme_root ) { $theme_root = WP_CONTENT_DIR . '/themes'; } elseif ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) { $theme_root = WP_CONTENT_DIR . $theme_root; } } return new WP_Theme( $stylesheet, $theme_root );}Changelog
Introduced in 3.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.