0

WRT - Hwo to turn off "get_parent_theme_file_path" in child-theme?

Twenty Seventeen parent theme.

I'm trying to make my child theme over-ride the file /inc/template-tags.php - both my parent theme and child theme have the file in place. (my child version has the adjustments)

The parent enqueues using:

require get_parent_theme_file_path( '/inc/template-tags.php' );

I have placed the code below in the child function.php

add_filter('parent_theme_file_path', function($path, $file) {
if ($file !== '/inc/template-tags.php') {
    return $path;
}

$path = get_stylesheet_directory() . '/' . $file;

return $path;
}, 10, 2);

Unfortunately the parent version of the file is still being used.. What am I missing to override with the child version?

many thanks in advance

1
  • 1
    The parent theme uses get_parent_theme_file_path() for a reason. This is a signal that you shouldn’t be replacing the whole file in a child theme. If you want to replace template tags then you should copy the just the function into your child theme and modify it there (if it’s pluggable) or create a new function and then replace the original function by copying the template that it’s used in and replacing the original function call with a call to yours. Commented Apr 30, 2019 at 5:10

1 Answer 1

2

WordPress applies ltrim on the $fileltrim( $file, '/' ), so it becomes inc/template-tags.php instead of /inc/template-tags.php.

So within your filter callback, you should use:

if ( $file !== 'inc/template-tags.php' )

UPDATE

my child version has the adjustments

I don't know if you made adjustments to pluggable or non-pluggable function(s) in the template file, but as pointed by @JacobPeattie, you should consider extending parent theme functions like so:

  • For pluggable functions such as twentyseventeen_posted_on, just copy the whole code and paste it in your functions.php file. And then just make your adjustments.

    Pluggable functions are defined like so, where the function is wrapped inside a conditional which checks whether a function with the same name already exists or not, and if not, the function is defined:

    if ( ! function_exists( 'function_name' ) ) :
        function function_name() {
            ...
        }
    endif;
    
  • For non-pluggable functions such as twentyseventeen_front_page_section, follow the same steps above (copy the function code to your functions.php file), but make sure to rename the function — e.g. my_theme_front_page_section or my_twentyseventeen_front_page_section.

    And after you renamed the function, make sure to change the name in template files where the function is being called. For example, twentyseventeen_front_page_section is being called in the front page (front-page.php) template, so you'd change twentyseventeen_front_page_section( null, $i ); to my_theme_front_page_section( null, $i ); (if you renamed the function to my_theme_front_page_section).

I could understand that it might be a hassle to do the renaming part, but particularly for pluggable functions, you don't need to override the template file using the parent_theme_file_path filter (or similar filter). The function(s) you're trying to modify might also actually have filters you can simply hook into to customize/change the function output, so be sure to check that. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.