Using get_stylesheet_uri to get the theme stylesheet URI in WordPress
The WordPress get_stylesheet_uri
function is a built-in function that retrieves the URL of the current theme’s stylesheet. This function is typically used to load the main stylesheet for a WordPress theme.
The functionality of the get_stylesheet_uri
function is to return the URI of the current theme’s stylesheet. This function is designed to work in conjunction with the WordPress theme system, which allows developers to create and modify the visual design and layout of a WordPress site.
When a theme is activated in WordPress, the get_stylesheet_uri
function can be invoked to fetch the URI of the stylesheet associated with that theme. This URI can then be used in various ways, such as linking to the stylesheet in the HTML header of a WordPress site.
It is important to note that the get_stylesheet_uri
function will return the URI of the current theme’s stylesheet, regardless of whether the stylesheet file actually exists. If the stylesheet file does not exist, the function will still return a URI, but attempting to access the stylesheet at that URI will result in a 404 error.
Parameters Accepted by get_stylesheet_uri Function
The get_stylesheet_uri
function in WordPress does not take any parameters. This function operates independently of any input parameters.
Return Value of get_stylesheet_uri Function
The get_stylesheet_uri
function provides the URI of the active theme’s stylesheet as a string. This returned value represents the location of the currently active theme’s stylesheet.
Examples
How to Load Active Theme’s Stylesheet
The most common usage of the get_stylesheet_uri
function is to load the active theme’s stylesheet.
function theme_styles() {
wp_enqueue_style( 'theme-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'theme_styles' );
In this example, the get_stylesheet_uri
function is used to get the URI of the active theme’s stylesheet. The wp_enqueue_style
function is then used to add this stylesheet to the WordPress queue of stylesheets to be loaded on the page. The add_action
function is used to hook this function to the wp_enqueue_scripts
action, which is the action that WordPress uses to load scripts and stylesheets.
How to Display the URI of the Active Theme’s Stylesheet
The get_stylesheet_uri
function can also be used to display the URI of the active theme’s stylesheet.
function display_stylesheet_uri() {
echo '<p>' . get_stylesheet_uri() . '</p>';
}
add_action( 'wp_footer', 'display_stylesheet_uri' );
In this example, the get_stylesheet_uri
function is used to get the URI of the active theme’s stylesheet. This URI is then echoed out within a paragraph element. The add_action
function is used to hook this function to the wp_footer
action, which is the action that WordPress uses to insert content into the footer of the page.
How to Conditionally Load a Stylesheet
The get_stylesheet_uri
function can be used in conditional statements to control when a stylesheet is loaded.
function conditional_stylesheet() {
if ( is_front_page() ) {
wp_enqueue_style( 'front-page-style', get_stylesheet_uri() );
}
}
add_action( 'wp_enqueue_scripts', 'conditional_stylesheet' );
In this example, the get_stylesheet_uri
function is used to get the URI of the active theme’s stylesheet. The wp_enqueue_style
function is then used to add this stylesheet to the WordPress queue of stylesheets to be loaded on the page, but only if the current page is the front page. The add_action
function is used to hook this function to the wp_enqueue_scripts
action, which is the action that WordPress uses to load scripts and stylesheets.
Conclusion
The get_stylesheet_uri
function in WordPress is a built-in function that returns the URI of the current theme’s stylesheet. This function is commonly used when enqueuing the main stylesheet for a WordPress theme, typically in the functions.php
file. It allows developers to easily reference the location of the theme’s main stylesheet, ensuring that the theme’s styles are correctly applied to the site. This function is particularly useful when working with child themes, as it will return the URI of the child theme’s stylesheet, not the parent theme’s.
Related WordPress Functions
- Using get_theme_file_uri to get a theme file URL in WordPress
- Retrieving theme details in WordPress using wp_get_theme
- Using get_stylesheet_directory to get the theme directory path in WordPress
- Retrieving the stylesheet directory URI in WordPress with get_stylesheet_directory_uri
- Retrieving the root URI of a theme in WordPress with get_theme_root_uri
- How to get the path to the themes directory in WordPress with get_theme_root
- How to add custom stylesheets to WordPress using wp_enqueue_style
- Getting the current theme directory path in WordPress with get_template_directory
- Getting the current WordPress theme directory URI with get_template_directory_uri