Using WordPress ‘get_parent_theme_file_uri()’ PHP function

The get_parent_theme_file_uri() WordPress PHP function retrieves the URL of a file in the parent theme.

Usage

get_parent_theme_file_uri( $file );

Example:
Input:

echo get_parent_theme_file_uri( 'style.css' );

Output:

https://example.com/wp-content/themes/parent-theme/style.css

Parameters

  • $file (string) (Optional): File to return the URL for in the template directory. Default: ”

More information

See WordPress Developer Resources: get_parent_theme_file_uri()

Examples

Load parent theme stylesheet

Load the parent theme’s stylesheet in your child theme.

function child_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_parent_theme_file_uri( 'style.css' ) );
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );

Load a custom CSS file from the parent theme

Load a custom CSS file named ‘custom.css’ from the parent theme.

function custom_enqueue_styles() {
    wp_enqueue_style( 'custom-style', get_parent_theme_file_uri( 'custom.css' ) );
}
add_action( 'wp_enqueue_scripts', 'custom_enqueue_styles' );

Display an image from the parent theme

Display an image named ‘logo.png’ from the parent theme’s ‘images’ directory.

$image_url = get_parent_theme_file_uri( 'images/logo.png' );
echo '<img src="' . esc_url( $image_url ) . '" alt="Logo">';

Load a JavaScript file from the parent theme

Load a JavaScript file named ‘script.js’ from the parent theme.

function custom_enqueue_scripts() {
    wp_enqueue_script( 'custom-script', get_parent_theme_file_uri( 'script.js' ), array(), false, true );
}
add_action( 'wp_enqueue_scripts', 'custom_enqueue_scripts' );

Get the URL of a font file in the parent theme

Get the URL of a font file named ‘custom-font.woff2’ from the parent theme’s ‘fonts’ directory.

$font_url = get_parent_theme_file_uri( 'fonts/custom-font.woff2' );