get_parent_theme_file_uri() WP 4.7.0
Retrieves the URL of the specified file of the parent theme.
The function is useful when you want to get a URL to a file of the parent theme, no matter where the function was called (in the parent or a child theme).
Use get_theme_file_uri() when you want to take a child theme into account (get its URL).
Use get_parent_theme_file_path() when you want to get a file path (not a URL).
Hooks from the function
Return
String. The URL of the file.
Usage
get_parent_theme_file_uri( $file );
- $file(string)
The filename to be found in the parent theme. For example
file.js
. You can specify a file with a directory:js/file.php
or/js/file.js
.If you leave the parameter empty, it will return the URL of the parent theme. See get_template_directory_uri().
Default: ''
Examples
#1 Get the URL of the parent theme file from the child theme
Let's say we need to include the js/my-script.js
file in the parent theme. But the code is used in the child theme.
wp_enqueue_script( 'my-script', get_parent_theme_file_uri( 'js/my-script.js' ) );
Changelog
Since 4.7.0 | Introduced. |
Code of get_parent_theme_file_uri() get parent theme file uri WP 5.6
function get_parent_theme_file_uri( $file = '' ) {
$file = ltrim( $file, '/' );
if ( empty( $file ) ) {
$url = get_template_directory_uri();
} else {
$url = get_template_directory_uri() . '/' . $file;
}
/**
* Filters the URL to a file in the parent theme.
*
* @since 4.7.0
*
* @param string $url The file URL.
* @param string $file The requested file to search for.
*/
return apply_filters( 'parent_theme_file_uri', $url, $file );
}