get_stylesheet_directory

Home » Functions » get_stylesheet_directory

Function Name: get_stylesheet_directory

WordPress themes typically have two main directories: the parent theme directory and the child theme directory. The get_stylesheet_directory function retrieves the path of the current active theme’s stylesheet directory. This function is particularly useful when you want to include files, such as images or JavaScript, in your theme without hard-coding the absolute path.

Here’s an example of how to use the function:

// Get the stylesheet directory URI
$stylesheet_dir_uri = get_stylesheet_directory_uri();

// Enqueue the custom script file
wp_enqueue_script( 'custom-script', $stylesheet_dir_uri . '/js/custom.js', array(), '1.0', true );

In this example, we are retrieving the current active theme’s stylesheet directory URI using the get_stylesheet_directory_uri function. We then enqueue a custom script file by passing the URI path to the wp_enqueue_script function.

Overall, the get_stylesheet_directory function is a handy WordPress function that can simplify the process of including files in your theme.

Learn More on WordPress.org

WordPress snippets using the get_stylesheet_directory function

Register an account to save your snippets or go Pro to get more features.