- WordPress Path Functions: List WordPress Theme, Plugin, Site URL and Absolute Path in Server – WordPress Tutorial
- About wordpress site
- About wordpress theme path function
- About wordpress plugin path function
- get_stylesheet_directory_uri() │ WP 1.5.0
- Return
- Usage
- Examples
- #1 Display an image from theme directory:
- #2 Loading css styles:
- #3 Example
- Changelog
- get_stylesheet_directory_uri() get stylesheet directory uri code WP 6.2.2
- Related Functions
- theme path (theme folder url)
- Link (URL)
- Theme files connection
- Get Theme Directory In WordPress
- get_template_directory_uri()
- get_stylesheet_directory_uri()
- get_stylesheet_uri()
- get_theme_root_uri()
- get_theme_root()
- get_theme_roots()
- get_stylesheet_directory()
- get_template_directory()
WordPress Path Functions: List WordPress Theme, Plugin, Site URL and Absolute Path in Server – WordPress Tutorial
When we are developing a wordpress theme or plugin, we have to get some wordpress path information. such as the absolute path of wordpress theme, plugin, style file or home url. In this tutoral, we will introduce some wordpress path functions to get these path or url information.
About wordpress site
It will return the home url of a site. For example:
To learn more on home_url(), you can read this tutorial.
This function returns the home url of a wordpress site, which may be not the same to home_url(). For example, if you created a wordpress site in a blog folder. You will see:
$url = site_url(); echo $url; //output:http://www.example.com/blog $url = home_url(); echo $url; //output:http://www.example.com
It returns the wordpress dashboard admin url.
$url = admin_url(); echo $url; //output: http://www.example.com/wp-admin/
This function returns the url of wp-content folder.
$url = content_url(); echo $url; //output: http://www.example.com/wp-content
This function returns the url of wp-includes folder.
$url = includes_url(); echo $url; //output: http://www.example.com/wp-includes/ $url = includes_url( '/js/'); echo $url; //output: http://www.example.com/wp-includes/js/
It returns the url of wordpress uploads folder.
$upload_dir = wp_upload_dir(); echo $upload_dir['baseurl']; //output: http://www.example.com/wp-content/uploads
About wordpress theme path function
It returns the url of wordpress themes folder.
This function returns the absolute path of themes folder in server.
This function returns the absolute path of current wordpress theme in server.
This function returns the url of current theme.
About wordpress plugin path function
This function returns the url of plugins folder.
This function is same to plugins_url().
It returns the absolute path of current plugin folder in server.
get_stylesheet_directory_uri() │ WP 1.5.0
Retrieve stylesheet directory URI. This function returns the URL to the current child theme if a child theme is used. If you want to return the URL to the root/mother theme, use get_template_directory_uri() instead.
Hooks from the function
Return
Usage
get_stylesheet_directory_uri();
Examples
#1 Display an image from theme directory:
#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 ( / ) Add Your Own Example
Changelog
get_stylesheet_directory_uri() get stylesheet directory uri code WP 6.2.2
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 ); >
Related Functions
theme path (theme folder url)
Link (URL)
- admin_url()
- attachment_url_to_postid()
- build_query()
- comment_link()
- content_url()
- get_comments_link()
- get_comments_pagenum_link()
- get_edit_post_link()
- get_edit_term_link()
- get_home_url()
- get_next_comments_link()
- get_next_posts_page_link()
- get_page_link()
- get_post_embed_url()
- get_post_permalink()
- get_post_type_archive_link()
- get_previous_posts_page_link()
- get_privacy_policy_url()
- get_rest_url()
- get_search_link()
- get_stylesheet_uri()
- get_year_link()
- home_url()
- includes_url()
- network_admin_url()
- network_home_url()
- plugin_dir_url()
- plugins_url()
- site_url()
- strip_fragment_from_url()
- the_post_thumbnail_url()
- url_to_postid()
- wc_get_cart_url()
- wp_extract_urls()
- wp_get_attachment_image_url()
- wp_get_upload_dir()
- wp_make_link_relative()
- wp_parse_url()
- wp_registration_url()
- wp_upload_dir()
- wp_validate_redirect()
Theme files connection
Get Theme Directory In WordPress
When you doing theme development there are times when you need to reference a file in the same folder of the current theme. You may need to include a file in your code or add a stylesheet to your website. There are many different ways you can include a file in your code, WordPress comes with a number of constants that you can use in your development to get paths to your wp-content folder or plugin folders.
WP_CONTENT_DIR // no trailing slash, full paths only WP_CONTENT_URL // full url WP_PLUGIN_DIR // full path, no trailing slash WP_PLUGIN_URL // full url, no trailing slash
These are hardcoded constants so if you want to reference a specific theme you will need to hardcode the theme name in your code.
$twentyThirteenTheme = WP_CONTENT_DIR . '/themes/twentythirteen/'; $twentyThirteenThemeStylesheet = WP_CONTENT_URL . '/themes/twentythirteen/style.css';
The problem you have with this is that if your theme name changes then it will break your code. WordPress comes with a number of useful functions you can use to get the current theme path or URL.
get_template_directory_uri() get_stylesheet_directory_uri() get_stylesheet_uri() get_theme_root_uri() get_theme_root() get_theme_roots() get_stylesheet_directory() get_template_directory()
get_template_directory_uri()
This function will return the URL of the current theme, it will not return a trailing slash. If you are using a child theme then this function will return the parent theme directory URL.
Use this function to include a new Stylesheet or Javascript file in your theme.
add_action('wp_enqueue_scripts', 'my_scripts_method'); ?>
get_stylesheet_directory_uri()
This function will return the URL for the current theme, if you are running a child theme then this will return the child theme directory URL.
This can be used exactly the same way as the get_template_directory_uri().
add_action('wp_enqueue_scripts', 'my_scripts_method'); ?>
get_stylesheet_uri()
The above functions will return the URL for the directory of the current theme, this function will return the URL of the current theme stylesheet URL. If you are using the child theme then this will return the child theme stylesheet URL.
get_theme_root_uri()
THis function will return the URL to the theme directory.
get_theme_root()
This function will return the file path of the themes directory without a trailing slash.
get_theme_roots()
Return an array of themes located in the themes directory.
get_stylesheet_directory()
This function will return the the full file path to the current theme directory, like the get_stylesheet_directory_uri() this will return the current theme even if it’s being used as the child theme.
Use this function to include files in your application from the current theme.
get_template_directory()
This function will return the full file path to the current theme directory, like the get_template_directory_uri() if you are using a child theme then this function will return the path to the parent theme directory.
Use this function to include files in your application from the current theme.