How to find the directory of your WordPress Theme in PHP

- by

To get the directory of the current theme (or child theme) you can use get_stylesheet_directory_uri(). Here’s how to use it.

Let’s assume that your WordPress installation lives in http://demo.com, and that your theme is located in a folder named “my-super-theme”. We can assume then that the full URL that points at

http://demo.com/wp-content/themes/my-super-theme/

The URL will of course be different for every user of your theme, so you can’t hard code this. Instead, you can use this handy function:

$myThemeDirectory = get_stylesheet_directory_uri();

This will give you the same URL as above. You can also link to files inside your theme’s directory, for example:

$myImage = get_stylesheet_directory_uri() . '/images/my-super-image.jpg';

get_stylesheet_directory_uri() will point to the directory of your theme or your child theme. If you’re using a child theme and want to explicitly link to the parent theme’s directory, you can use get_template_directory_uri().

From the WordPress Codex:



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.