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

0

#1 Get the file URL of the parent theme from the child theme

Suppose we want to connect the file js/my-script.js from the parent theme. But the code is called in the child theme.

wp_enqueue_script( 'my-script', get_parent_theme_file_uri( 'js/my-script.js' ) );

Changelog

Since 4.7.0 Introduced.

get_parent_theme_file_uri() code WP 6.5.2

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 );
}