How to override a parent file in a child Wordpress theme when parent file is being required in functions.php
Asked Answered
E

3

9

I'm rather new to designing and coding Wordpress themes and have recently started a child theme based on the Sydney theme that's offered free in Wordpress. I have hit a snag I can't work out.

There's a PHP file being called through the parent's functions.php that I need to customize and need to be untouched when I update the parent theme. This file is called template-tags.php and is located inside an "inc" folder. Now, supposedly, according to the Wordpress codex (which I have consulted before asking here), any duplicate files on the child theme (except the functions.php) will automatically override those in the parent theme. I don't know if this works as well for files inside folders but I think the fact that this particular file is being called in the parent's functions.php makes it so that it is the parent's file that is being loaded. I am assuming this because the same goes for the style CSS and JS script files that I had to enqueue in the child theme in order to get my own customized versions in.

However, there is nothing in the Wordpress codex that explains how to override a file that is being already called in the parent's functions.php. And I can't find anything anywhere else (not even in these questions: WordPress child theme override a parent theme include, Overiding parent theme functions with a Child theme in WordPress, How to override a not plugable parent theme function from a non function.php file?, Wordpress child theme - functions.php copy - cannot "redeclare") that helps me specifically.

So, to sum it all up:

  • The parent theme has an "inc" folder with the file template-tags.php in it.
  • My child theme also has an "inc" folder with the file template-tags.php in it.
  • The parent theme's functions.php has the following code that calls its file:

    require get_template_directory() . '/inc/template-tags.php';
    
  • Anything I try to add in my child's functions.php file that calls its template-tags.php file will result in an error because obviously WP can't require the same thing twice.

Short of commenting out the call in the parent's functions.php, which does't seem to me a practical solution but more of a work-around, I have no idea what else to do.

I would appreciate any input at this time. Thanks in advance!

Experientialism answered 13/9, 2015 at 20:48 Comment(3)
its good practice to allow child theme files to override the parent template. But if the developer has used require you cannot slip your own file in there, it sounds like a page of functions anyway. You might see where the functions you want to override hook into and you can unhook them and hook your own.Irade
Hi, David, thanks for the answer. If I'm understanding you correctly, you recommend to link my custom file where the parent's file is linked. If this is so, I could also cut that link in the parent's file and the link in my child's functions.php would be the only one. But then I would have to keep that in mind and go in and do it every time the theme gets upgraded. And it seems this one does often. I started middle of last week and already it had an upgrade today; the 15th, because it's on version 1.15. I would prefer to avoid having to always have to do this. Thanks!Experientialism
no im stating the way it is :) but what would could be possible is to look at each function individually and unhook them if possible, for example if a function is hooked to init, unhook it and hook your own. but judging by the name of the file, template tags - these are used in templates? you could just create your own templates using your own tags, etc...Irade
B
11

Hello @QuestionerNo27,

It's been a long time since you asked this but I've also been looking for an answer for 2 days as I was facing the same issue. There is the answer, simple like "Hi":

  1. You dont need the inc folder in your child theme nor the template-tags.php file.
  2. You just have to copy the function(s) you want to override from your parent template-tags.php and paste it into your child theme functions.php In my case, I wanted to override function mytheme_posted_on() from my parent template-tags.php

And it's now working. Thanks to https://wordpress.org/support/topic/inc-folder-in-child-theme (Stephencottontail answer)

Balladeer answered 22/8, 2016 at 23:41 Comment(1)
As per my understanding, the child theme only extends the parent theme and adds more functions. If you only copy (and not cut) the function, then the server will throw an error because it tries to load the same function multiple times. Am I missing something ?Undercurrent
C
4

I had a similar problem where I wanted to make changes to the layout of the header in my child theme within the template-tags.php.

The solution that worked for me was to use the following line in my functions.php file in my child theme: require get_stylesheet_directory() . '/inc/template-tags.php';

I was using the original line from the the Parent them in functions.php: require get_template_directory() . '/inc/template-tags.php';

Using the function get_template_directory() was not working because this function will always return the parent theme directory.

Using the function get_stylesheet_directory() worked because it retrieves the Child Theme directory.

Yes I know the name of the function "get_stylesheet_directory()" is not very intuitive but it it actually will return the Child Theme directory location.

It took a while to work this out by searching online. I found this reference online that helped me in my search: How to override a parent file in a child Wordpress theme when parent file is being required in functions.php

Caustic answered 18/4, 2016 at 6:59 Comment(1)
hey that link is this question, I feel very confusedGuajardo
G
0

you should use

get_stylesheet_directory() that refers to active theme directory

instead of get_template_directory() that refers to parent theme directory

https://www.csestack.org/overwrite-wordpress-php-file-subfolder-child-theme/

Gillyflower answered 22/5, 2022 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.