It sometimes becomes necessary to directly access files within the active theme's directory. Binary file loading, PHP or HTML includes, and iteration over custom file structures (as used in some theme frameworks) are some of the reasons for using direct access.
You can access the STYLESHEETPATH
constant from any PHP file in your theme. The STYLESHEETPATH
constant is defined when WordPress first loads.
To give you an idea of how the constant works, consider the case where you want to load a file containing some variable declarations for your theme. Create a new file in your theme's directory called config-variables.php
, and add the following code to it:
<?php $blue = 1; $red = 2; $green = 3;
Next, open up your theme's header file—header.php—and add the following code at the very beginning of the file:
<?php include (STYLESHEETPATH . '/config-variables.php '); ?>
Now, anywhere inside of your theme, you'll be able to access the variables defined within config-variables.php
.
The STYLESHEETPATH
constant contains the absolute directory path to the file system location that contains the active theme. This is true for both regular themes and child themes. The STYLESHEETPATH
constant does not contain a trailing slash, so one will need to be appended when accessing individual files within the directory.