[wordpress] How to get child theme / main theme path and URI?

Since WordPress 4.7, these functions are available:

get_theme_file_uri() – if using a child theme, it will return the child theme’s full URI. Otherwise, it will return the main theme full URI.
e.g.

echo get_theme_file_uri();
// www.site.com/wp-content/themes/twentytwenty-child

It’s also possible to send a subfolder/file as parameter, like so:

echo get_theme_file_uri('images/logo.png');
// www.site.com/wp-content/themes/twentytwenty-child/images/logo.png

get_theme_file_path() – same as get_theme_file_uri() but will return the full path of the child theme or main theme.

echo get_theme_file_path();
// C:\wordpress/wp-content/themes/twentytwenty-child

For child theme usage, there are the following functions:
get_parent_theme_file_uri()
get_parent_theme_file_path()

Reference:
https://developer.wordpress.org/themes/basics/linking-theme-files-directories/#linking-to-theme-directories

Leave a comment

Your email address will not be published. Required fields are marked *