How to get the URI of a WordPress theme folder
In the article about child themes I published the code snippet for functions.php, a file that is essential to how themes work. In particular I used the get_template_directory_uri function.

In the article about child themes I published the code snippet for functions.php.
This file is essential to how themes work.
In particular I used the get_template_directory_uri function.
Contents
- What is the get_template_directory_uri function for?
- And how do you get the URI of the child theme folder?
- What are these functions for?
- How can I use them?
What is the get_template_directory_uri function for?
The function returns a string containing the URI of the theme folder:
<?php
echo get_template_directory_uri();If you run the file as a page template, it returns the path to the parent folder when you are using a child theme.
// output of the previous snippet
https://piaccapi.it/wp-content/themes/twentynineteenNote that there is no trailing slash.
And how do you get the URI of the child theme folder?
There is an equivalent function: get_stylesheet_directory_uri.
This function always returns the URI of the currently active theme, whether it is a child theme or a parent theme.
If we swapped this function into the previous code we would get the following result:
// output of the previous snippet
https://piaccapi.it/wp-content/themes/twentynineteen-childWhat are these functions for?
These functions are very useful when you need to add a CSS or JavaScript file to a page, as in the following snippet:
<?php
/**
* Functions for child theme.
*
* @package twentynineteen-child
*/
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' );
/**
* Enqueue the parent theme style.css
*/
function enqueue_parent_theme_style() {
$theme = wp_get_theme();
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css', array(), $theme->get( 'Version' ) );
}The snippet above shows the function in use: we are including the parent theme's style.css in a child theme, something that does not happen automatically when the child theme is created.
How can I use them?
For example, if your workflow uses two different environments, development and production, you can move the site's code without having to edit the paths in the various files that make up your theme every time, whether it is a child theme or a parent theme.