Paulund
2013-11-18 #wordpress

Get Theme Directory In WordPress

When you doing theme development there are times when you need to reference a file in the same folder of the current theme. You may need to include a file in your code or add a stylesheet to your website. There are many different ways you can include a file in your code, WordPress comes with a number of constants that you can use in your development to get paths to your wp-content folder or plugin folders.

WP_CONTENT_DIR  // no trailing slash, full paths only
WP_CONTENT_URL  // full url 
WP_PLUGIN_DIR  // full path, no trailing slash
WP_PLUGIN_URL  // full url, no trailing slash

These are hardcoded constants so if you want to reference a specific theme you will need to hardcode the theme name in your code.

$twentyThirteenTheme = WP_CONTENT_DIR . '/themes/twentythirteen/';
$twentyThirteenThemeStylesheet = WP_CONTENT_URL . '/themes/twentythirteen/style.css';

The problem you have with this is that if your theme name changes then it will break your code. WordPress comes with a number of useful functions you can use to get the current theme path or URL.


get_template_directory_uri()
get_stylesheet_directory_uri()
get_stylesheet_uri()
get_theme_root_uri()
get_theme_root()
get_theme_roots()
get_stylesheet_directory()
get_template_directory()

get_template_directory_uri()

This function will return the URL of the current theme, it will not return a trailing slash. If you are using a child theme then this function will return the parent theme directory URL.


<?php echo get_template_directory_uri(); ?>

Use this function to include a new Stylesheet or Javascript file in your theme.


<?php
function my_scripts_method() {
	wp_enqueue_script(
		'custom_script',
		get_template_directory_uri() . '/js/custom_script.js',
		array('jquery')
	);
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

get_stylesheet_directory_uri()

This function will return the URL for the current theme, if you are running a child theme then this will return the child theme directory URL.


<?php echo get_stylesheet_directory_uri(); ?>

This can be used exactly the same way as the get_template_directory_uri().


<?php
function my_scripts_method() {
	wp_enqueue_script(
		'custom_script',
		get_stylesheet_directory_uri() . '/js/custom_script.js',
		array('jquery')
	);
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

get_stylesheet_uri()

The above functions will return the URL for the directory of the current theme, this function will return the URL of the current theme stylesheet URL. If you are using the child theme then this will return the child theme stylesheet URL.


<?php echo get_stylesheet_uri(); ?>

get_theme_root_uri()

THis function will return the URL to the theme directory.


<?php echo get_theme_root_uri(); ?>

get_theme_root()

This function will return the file path of the themes directory without a trailing slash.


<?php echo get_theme_root(); ?>

get_theme_roots()

Return an array of themes located in the themes directory.


<?php echo get_theme_roots(); ?> 

get_stylesheet_directory()

This function will return the the full file path to the current theme directory, like the get_stylesheet_directory_uri() this will return the current theme even if it's being used as the child theme.


<?php echo get_stylesheet_directory() ?>

Use this function to include files in your application from the current theme.


<?php include( get_stylesheet_directory() . '/includes/myfile.php'); ?>

get_template_directory()

This function will return the full file path to the current theme directory, like the get_template_directory_uri() if you are using a child theme then this function will return the path to the parent theme directory.


<?php echo get_template_directory() ?>

Use this function to include files in your application from the current theme.


<?php include( get_template_directory() . '/includes/myfile.php'); ?>