On this page
get_theme_file_uri( string $file = '' ): string
Retrieves the URL of a file in the theme.
Description
Searches in the stylesheet directory before the template directory so themes which inherit from a parent theme can just override one file.
Parameters
$filestring Optional-
File to search for in the stylesheet directory.
Default:
''
Return
string The URL of the file.
Source
File: wp-includes/link-template.php. View all references
function get_theme_file_uri( $file = '' ) {
$file = ltrim( $file, '/' );
if ( empty( $file ) ) {
$url = get_stylesheet_directory_uri();
} elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
$url = get_stylesheet_directory_uri() . '/' . $file;
} else {
$url = get_template_directory_uri() . '/' . $file;
}
/**
* Filters the URL to a file in the theme.
*
* @since 4.7.0
*
* @param string $url The file URL.
* @param string $file The requested file to search for.
*/
return apply_filters( 'theme_file_uri', $url, $file );
}
Hooks
- apply_filters( 'theme_file_uri',
string $url ,string $file ) -
Filters the URL to a file in the theme.
Related
Uses
| Uses | Description |
|---|---|
| get_stylesheet_directory_uri() wp-includes/theme.php | Retrieves stylesheet directory URI for the active theme. |
| get_stylesheet_directory() wp-includes/theme.php | Retrieves stylesheet directory path for the active theme. |
| get_template_directory_uri() wp-includes/theme.php | Retrieves template directory URI for the active theme. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| _wp_theme_json_webfonts_handler() wp-includes/script-loader.php | Runs the theme.json webfonts handler. |
| get_block_editor_theme_styles() wp-includes/block-editor.php | Creates an array of theme styles to load into the block editor. |
| register_block_script_handle() wp-includes/blocks.php | Finds a script handle for the selected block metadata field. It detects when a path to file was provided and finds a corresponding asset file with details necessary to register the script under automatically generated handle name. It returns unprocessed script handle otherwise. |
| register_block_style_handle() wp-includes/blocks.php | Finds a style handle for the block metadata field. It detects when a path to file was provided and registers the style under automatically generated handle name. It returns unprocessed style handle otherwise. |
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_theme_file_uri