Blog Details


How to retrieve the path to a file within a wordpress theme

There are two types of themes with which a developer may be working when using WordPress themes. He may use a parent or a child theme. In WordPress, there are different functions that can be used to get paths to files, when using one of the above themes. Sometimes, finding a path can be difficult, despite visiting and revisiting many directories. Especially when you are modifying your WordPress site, not knowing where to find your files can be annoying. The following functions help find those paths in a quick manner.

Template Functions

The template functions can be put to use when resources are not overridden by a child theme. The following function is used to retrieve the absolute path to the directory of the current theme. This function may not be of help if you are using a child theme. In case a child theme is used, the function will only refer to the parent theme. The function is given as under:


It returns an absolute server path and does not return the URL. It does not output a trailing slash. It can be used for including PHP files. For example, the return value of the function may look like this:


get theme root _()

In case you wish to retrieve URI for the directory of the current theme, the following function can be used:

get_template_directory_uri ()

It cannot be used for loading PHP files. The function can be used if you wish to recover a fully formed URI, for links or images.

Stylesheet Functions

Stylesheet functions are intended to be used when working with child themes. When using child themes, these function will refer only to the child and not the parent theme. The following function retrieves an absolute path to the directory containing the style sheet. The function can be used for loading PHP files

The output will not include a trailing slash. The function is given hereunder:


However, if you wish to retrieve a fully formatted URI for the currently activated theme, the following function can be used.

get_stylesheet_directory_uri( )

Finding file location for images

Another way of finding paths for images is by locating the images on the Media Library that is found in the WordPress admin panel. The full URL of the image is visible when selecting and clicking on the image. It is also possible to find image or file locations by using the inspector of elements. Though the above steps look simple, many beginner developers on WordPress finds it difficult to locate paths to files and directories, due to lack of knowledge of these easy functions.

One of more of these functions can be used to obtain paths for finding files in a WordPress theme. These functions return paths to files and images and can also be used for referencing additional style sheets and JavaScript.