Using WordPress ‘parent_theme_file_path’ PHP filter

The parent_theme_file_path WordPress PHP filter allows you to modify the file path of a specific file in the parent theme.

Usage

add_filter('parent_theme_file_path', 'your_function_name', 10, 2);

function your_function_name($path, $file) {
    // your custom code here
    return $path;
}

Parameters

  • $path (string) – The original file path.
  • $file (string) – The requested file to search for.

More information

See WordPress Developer Resources: parent_theme_file_path

Examples

Change a specific file path

Change the file path for single.php in the parent theme.

add_filter('parent_theme_file_path', 'change_single_file_path', 10, 2);

function change_single_file_path($path, $file) {
    if ($file == 'single.php') {
        $path = get_template_directory() . '/custom/single.php';
    }
    return $path;
}

Add a prefix to all file paths

Add a custom prefix to all file paths in the parent theme.

add_filter('parent_theme_file_path', 'add_prefix_to_file_path', 10, 2);

function add_prefix_to_file_path($path, $file) {
    $path = get_template_directory() . '/custom-' . $file;
    return $path;
}

Change the path for all template files

Change the path for all template files in the parent theme to a custom folder.

add_filter('parent_theme_file_path', 'change_template_path', 10, 2);

function change_template_path($path, $file) {
    if (preg_match('/\.php$/', $file)) {
        $path = get_template_directory() . '/custom_templates/' . $file;
    }
    return $path;
}

Exclude a specific file from being loaded

Prevent archive.php from being loaded in the parent theme.

add_filter('parent_theme_file_path', 'exclude_archive_file', 10, 2);

function exclude_archive_file($path, $file) {
    if ($file == 'archive.php') {
        $path = '';
    }
    return $path;
}

Change the path for files in a specific folder

Change the path for all files inside the blocks folder in the parent theme.

add_filter('parent_theme_file_path', 'change_blocks_folder_path', 10, 2);

function change_blocks_folder_path($path, $file) {
    if (strpos($file, 'blocks/') !== false) {
        $path = get_template_directory() . '/custom_blocks/' . $file;
    }
    return $path;
}