get_stylesheet_directory()
Gets the absolute path to the child theme or parent theme (if the child theme is NOT used). Does not contain a trailing slash /
.
This function points to the folder where the current theme's stylesheet file is located.
For example: if the parent theme is currently activated, we get the path to it, and if the child theme is activated, we get the path to the child theme.
Thus, this function can be used in the parent theme to get the path to the child theme when it is activated.
Use get_template_directory() when you need to get the path to the parent theme from the child theme.
Use get_stylesheet_directory_uri() when you need to get the URL, not the path.
Hooks from the function
Returns
String
. Absolute path to the theme directory: /home/example.com/www/wp-content/themes/theme
.
Usage
get_stylesheet_directory();
Examples
#1 Demo
echo get_stylesheet_directory(); // return: /home/k/kama/example.com/public_html/wp-content/themes/themename
#2 Connect the myfile.php file for current theme:
The example shows how to use the function. Use this code in the parent theme to support the child theme:
require_once get_stylesheet_directory(). '/includes/myfile.php';
Changelog
Since 1.5.0 | Introduced. |
Since 6.4.0 | Memoizes filter execution so that it only runs once for the current theme. |
Since 6.4.2 | Memoization removed. |
get_stylesheet_directory() get stylesheet directory code WP 6.8.1
function get_stylesheet_directory() { $stylesheet = get_stylesheet(); $theme_root = get_theme_root( $stylesheet ); $stylesheet_dir = "$theme_root/$stylesheet"; /** * Filters the stylesheet directory path for the active theme. * * @since 1.5.0 * * @param string $stylesheet_dir Absolute path to the active theme. * @param string $stylesheet Directory name of the active theme. * @param string $theme_root Absolute path to themes directory. */ return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root ); }