get_theme_root() WordPress Function

The get_theme_root() function in WordPress returns the absolute path to the directory of the current theme. This is useful for finding the path to template files or other theme-specific resources.

get_theme_root( string $stylesheet_or_template = '' ) #

Retrieves path to themes directory.


Description

Does not have trailing slash.


Top ↑

Parameters

$stylesheet_or_template

(string)(Optional) The stylesheet or template name of the theme. Default is to leverage the main theme root.

Default value: ''


Top ↑

Return

(string) Themes directory path.


Top ↑

Source

File: wp-includes/theme.php

function get_theme_root( $stylesheet_or_template = '' ) {
	global $wp_theme_directories;

	$theme_root = '';

	if ( $stylesheet_or_template ) {
		$theme_root = get_raw_theme_root( $stylesheet_or_template );
		if ( $theme_root ) {
			// Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
			// This gives relative theme roots the benefit of the doubt when things go haywire.
			if ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
				$theme_root = WP_CONTENT_DIR . $theme_root;
			}
		}
	}

	if ( ! $theme_root ) {
		$theme_root = WP_CONTENT_DIR . '/themes';
	}

	/**
	 * Filters the absolute path to the themes directory.
	 *
	 * @since 1.5.0
	 *
	 * @param string $theme_root Absolute path to themes directory.
	 */
	return apply_filters( 'theme_root', $theme_root );
}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More