register_theme_directory()WP 2.9.0

Register a directory that can contain themes.

The $directory parameter specifies the path to the theme folder with no slash at the end. The specified path is added to the global array of $wp_theme_directories, which is then used when retrieving the path/URL to the WordPress themes folder.

No Hooks.

Return

true|false. True if successfully registered a directory that contains themes, false if the directory does not exist.

Usage

register_theme_directory( $directory );
$directory(string) (required)
Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR.

Examples

0

#1 Make the plugin folder as a folder to the WordPress themes

/*
 * For the structure of the plugin of this kind:
 * 
 * /my-plugin
 *    /my-plugin.php
 *    /themes/
 *
 * This code should be placed in the file my-plugin.php.
 */
register_theme_directory( dirname( __FILE__ ) . '/themes' );

Notes

  • Global. Array. $wp_theme_directories

Changelog

Since 2.9.0 Introduced.

register_theme_directory() code WP 6.1.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;
}