register_theme_directory()WP 2.9.0

Регистрирует директорию в которой могут находится темы.

В параметре $directory указывается путь до папки с темами без слша на конце. Указанный путь добавляется в глобальный массив $wp_theme_directories, который затем используется при получении пути и URL на папку тем WordPress.

Хуков нет.

Возвращает

true|false.

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

register_theme_directory( $directory );
$directory(строка) (обязательный)
Пусть до папки с темами. Можно передать два варианта:
- полный путь до папки
- относительный путь от папки WP_CONTENT_DIR.

Примеры

0

#1 Сделаем папку плагина папкой для тем

/*
 * Для структуры плагина такого вида:
 * 
 * /my-plugin
 *    /my-plugin.php
 *    /themes/
 *
 * Этот код нужно разместить в файле my-plugin.php.
 */
register_theme_directory( dirname( __FILE__ ) . '/themes' );

Заметки

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

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

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

Код register_theme_directory() WP 6.7.1

function register_theme_directory( $directory ) {
	global $wp_theme_directories;

	if ( ! file_exists( $directory ) ) {
		// Try prepending as the theme directory could be relative to the content directory.
		$directory = WP_CONTENT_DIR . '/' . $directory;
		// If this directory does not exist, return and do not register.
		if ( ! file_exists( $directory ) ) {
			return false;
		}
	}

	if ( ! is_array( $wp_theme_directories ) ) {
		$wp_theme_directories = array();
	}

	$untrailed = untrailingslashit( $directory );
	if ( ! empty( $untrailed ) && ! in_array( $untrailed, $wp_theme_directories, true ) ) {
		$wp_theme_directories[] = $untrailed;
	}

	return true;
}