WordPress как на ладони

get_theme_root()WP 1.5.0

Получает абсолютный путь папки (каталога) с темами WordPress. Слэш на конце отсутствует.

Пример пути: /home/example.com/public_html/wp-content/themes.

Основа для: get_template_directory()
1 раз — 0.00001 сек (скорость света) | 50000 раз — 0.01 сек (скорость света) | PHP 7.4.8, WP 5.6.2
Хуки из функции

Возвращает

Строку. Путь до директории шаблонов.

Использование

$path = get_theme_root( $stylesheet_or_template );
$stylesheet_or_template(строка)

Название темы или стилей темы, родительскую папку темы которой нужно получить.

В 99% случаев этот параметр бесполезный. Он нужен для случаев когда для тем указано несколько папок и нам нужно получить родительскую папку всех тем, в которой лежит указанная в этом параметре тема. См. global $wp_theme_directories.
По умолчанию: ''

Примеры

1

#1 Количество подкаталогов в каталоге темы

function display_themes_subdirs_count_info(){

  $theme_root = get_theme_root();
  $files_array = glob( "$theme_root/*", GLOB_ONLYDIR );

  echo count( $files_array ) . " подкаталогов в каталоге: $theme_root";
}
0

#2 Получим полный путь до каталога тем:

echo get_theme_root();             //> /home/k/foo/example.com/www/wp-content/themes
echo get_theme_root( 'my-theme' ); //> /home/k/foo/example.com/www/wp-content/themes

Заметки

  • Global. Массив. $wp_theme_directories

Список изменений

С версии 1.5.0 Введена.

Код get_theme_root() WP 6.5.2

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 );
}
2 комментария
    Войти