Using WordPress ‘get_parent_theme_file_path()’ PHP function

The get_parent_theme_file_path() WordPress PHP function retrieves the path of a file in the parent theme.

Usage

$path = get_parent_theme_file_path('inc/example-file.php');

Parameters

  • $file (string, optional) – File to return the path for in the template directory. Default: ''

More information

See WordPress Developer Resources: get_parent_theme_file_path

Examples

Load a custom PHP file from the parent theme

Load custom-functions.php file located in the parent theme’s inc folder:

require get_parent_theme_file_path('/inc/custom-functions.php');

Check if a file exists in the parent theme

Check if my-custom-file.php exists in the parent theme’s root folder:

if (file_exists(get_parent_theme_file_path('my-custom-file.php'))) {
    // File exists, do something
}

Load a CSS file from the parent theme

Enqueue a custom-styles.css file located in the parent theme’s assets/css folder:

function enqueue_parent_styles() {
    wp_enqueue_style('custom-styles', get_parent_theme_file_uri('/assets/css/custom-styles.css'));
}
add_action('wp_enqueue_scripts', 'enqueue_parent_styles');

Create a function that returns the parent theme’s file path

Create a function get_custom_file_path() to retrieve the parent theme’s file path:

function get_custom_file_path($file) {
    return get_parent_theme_file_path($file);
}

// Use the function to get the path of a custom file
$path = get_custom_file_path('/inc/custom-file.php');

Load a JS file from the parent theme

Enqueue a custom-scripts.js file located in the parent theme’s assets/js folder:

function enqueue_parent_scripts() {
    wp_enqueue_script('custom-scripts', get_parent_theme_file_uri('/assets/js/custom-scripts.js'), array('jquery'), false, true);
}
add_action('wp_enqueue_scripts', 'enqueue_parent_scripts');