最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I override a require() used in functions.php of parent theme to my child theme

programmeradmin3浏览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.

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.

Share Improve this question edited Jan 21, 2020 at 11:55 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Jan 21, 2020 at 11:30 Javed AkhtarJaved Akhtar 133 bronze badges 7
  • You can try to apply this filter developer.wordpress/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 Commented Jan 21, 2020 at 11:45
  • 1 @VitautsStočka that won't work for a require statement, only get_template_part – Tom J Nowell Commented Jan 21, 2020 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 tag from your Q as your site isn't an official WordPress Foundation website such as developer.wordpress, just a self hosted site – Tom J Nowell Commented Jan 21, 2020 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 Commented Jan 21, 2020 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 Commented Jan 21, 2020 at 13:08
 |  Show 2 more comments

1 Answer 1

Reset to default 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
发布评论

评论列表(0)

  1. 暂无评论