get_stylesheet_directory_uri()
Gets the URL of the current theme (child if it is used or parent). Does not contain / at the end. Considers SSL.
The returned URL does not contain a slash (/) at the end: http://example.com/wp-content/themes/twentyten.
The result of the function will be an address starting with http:// or https:// for SSL.
Use get_template_directory_uri() to get the link to the parent theme directory, even if a child is used.
This function is analogous to get_bloginfo( 'stylesheet_directory' ).
To get the URL of the theme styles: the style.css file, you can use the special function get_stylesheet_uri().
If you need to include in a PHP file, then you need a path, not a link; use the function: get_stylesheet_directory().
Hooks from the function
Returns
String. URL without a slash (/) at the end.
Usage
$theme_url = get_stylesheet_directory_uri();
Examples
#1 Display an image from theme directory:
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />
#2 Loading css styles:
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
function my_scripts_method() {
$url = get_stylesheet_directory_uri() . '/js/custom_script.js';
wp_enqueue_script( 'custom_script', $url, [ 'jquery' ], '1.0', true );
} #3 Example
Website URL: https://example.com/
Active theme folder: mytheme-child - this is child theme and the parent theme is mytheme.
This function returns the following string:
https://example.com/wp-content/themes/mytheme-child
NOTE: without trailing slash (/)
Changelog
| Since 1.5.0 | Introduced. |
get_stylesheet_directory_uri() get stylesheet directory uri code WP 6.9
function get_stylesheet_directory_uri() {
$stylesheet = str_replace( '%2F', '/', rawurlencode( get_stylesheet() ) );
$theme_root_uri = get_theme_root_uri( $stylesheet );
$stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
/**
* Filters the stylesheet directory URI.
*
* @since 1.5.0
*
* @param string $stylesheet_dir_uri Stylesheet directory URI.
* @param string $stylesheet Name of the activated theme's directory.
* @param string $theme_root_uri Themes root URI.
*/
return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
}