Function and usage of get_parent_theme_file_uri() in wordpress


Answers ( 1 )


    The get_parent_theme_file_uri() function in WordPress is a useful tool for theme developers, especially when working with child themes. Here's a detailed explanation of its function and usage:


    • get_parent_theme_file_uri( string $file = '' ): string
    • Purpose: This function retrieves the URL of a file located in the parent theme's directory. It's particularly useful in child themes where you might need to reference files from the parent theme.


    • $file (string, optional): Specifies the path to the file within the parent theme directory for which you want to retrieve the URL. This path is relative to the parent theme root.
    • Default: An empty string (''), which implies the function will return the URL of the parent theme's root directory if no specific file is specified.


    • Type: string
    • Description: The function returns the URL of the specified file in the parent theme. If no file is specified, it returns the URL of the parent theme's root directory.

    Sample Usage:

    1. Basic Usage: To get the URL of the parent theme directory:

      $parent_theme_url = get_parent_theme_file_uri();
    2. Specific File in Parent Theme: To get the URL of a specific file (e.g., a CSS file named style.css) in the parent theme:

      $parent_style_url = get_parent_theme_file_uri( 'style.css' );
    3. Using with Enqueue Functions: This function is often used in conjunction with wp_enqueue_style() or wp_enqueue_script() to properly link stylesheets or JavaScript files from the parent theme in a child theme. For example:

      function my_theme_enqueue_styles() {
          $parent_style_url = get_parent_theme_file_uri( 'style.css' );
          wp_enqueue_style( 'parent-style', $parent_style_url );
      add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );


    • This function is particularly useful for child themes, as it always references the parent theme, regardless of whether the current theme is a child theme or the parent theme itself.
    • It's a part of WordPress's theme file path retrieval functions, which also includes functions like get_theme_file_uri() and get_stylesheet_uri(), each serving different purposes in theme development.


    get_parent_theme_file_uri() is a straightforward and efficient way to reference files in the parent theme, ensuring that the correct URLs are used, especially in the context of child theme development in WordPress.

Leave an answer