0

I have created a child theme for a Theme, Every thing works fine, except there are some files being required in functions.php of parent theme which I want to override it from the child theme. They have used get_template_directory_uri();instead of get_stylesheet_directory_uri();. So it's not overriding it. Code used in function.php is:

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

I can't change the parent theme or unhook from child theme. What is the best solution to use the child one as I can't call both at a time, resulting into fatal error. I have checked some answers on stackoverflow, but they do not answer exactly.

Child theme - Overriding 'require_once' in functions.php

This answer suggest to use stylesheet instead of template, but I cannot change parent theme.

  • You can try to apply this filter developer.wordpress.org/reference/hooks/template_directory to fool parent theme into requiring file from your child theme. However it may not work as it could break all other parent themes requires. Ideally you should activate this filter just at the end of your functions.php and remove it after last of your changed files is required by parent theme. – Vitauts Stočka Jan 21 at 11:45
  • 1
    @VitautsStočka that won't work for a require statement, only get_template_part – Tom J Nowell Jan 21 at 11:51
  • Javed, the question you linked to isn't the same as the OP can modify the parent theme but doesn't know how, something you've stated is not possible for you. I also removed the wordpress.org tag from your Q as your site isn't an official WordPress Foundation website such as developer.wordpress.org, just a self hosted site – Tom J Nowell Jan 21 at 11:55
  • @TomJNowell If parent theme code is exactly as OP example, hook works just fine. Tested it myself. You can see that file path for require (which of course is language construct and not hookable by itself) is built by prepending it with get_template_directory(), which is hookable. Child functions.php is called before parent functions.php, so if at the end of child's functions.php I add hook on template_directory to return get_stylesheet_directory() and then remove it at end of my replaced inc file, it works just fine. Parent is fooled to load my inc instead of its own. – Vitauts Stočka Jan 21 at 13:04
  • That would replace all instances for the parent theme, meaning any stylesheets and scripts or custom template loaders for the parent theme would be broken. Additionally, since a lot of things happen on hooks and events, it would impact code from the child themes functions.php too – Tom J Nowell Jan 21 at 13:08
3

This is not possible, require is a PHP language construct, it's not a WordPress function, and can't be filtered or overriden via WordPress APIs

These are your options:

If the functionality you want to remove is implemented via actions and filters then:

  • Unhook the things you don't want from that file
  • Add new hooks that happen after them that attempt to undo what they did

Otherwise, your only options are to:

  • Modify the parent theme
  • Fork the parent theme
  • Choose a new parent theme
  • Raise a support ticket with the authors to get it changed to support actions and filters
| improve this answer | |
  • But I can't unhook it also – Javed Akhtar Jan 21 at 11:56
  • 1
    As I said, it is not possible without modifying or replacing the parent theme – Tom J Nowell Jan 21 at 11:56
  • Hey Tom! Here what do you mean by 'Fork the parent theme' ? #1 fork it on e.g. Github, doing the custom mods and do a pull request #2 or some other automated deployment solution, etc. that i don't know of? – Viktor Borítás Oct 13 at 15:30
  • By fork I mean to duplicate the parent theme, change its name and folder name, and maintain it as a brand new theme – Tom J Nowell Oct 13 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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