0

In the 10+ years that I have been working with WordPress child themes, I have never seen a problem with a template file being copied into a child theme and customized, until now. There were function calls in the parent theme header.php that caused a fatal error in a student's child theme. The theme author removed the function from the parent theme, so the call threw an error.

Removing the offending function calls fixed the error, but it did highlight a hole in the idea that a child theme protects the customizations from updates. It protects the customizations from being overwritten, but it doesn't protect the site from errors.

My question is two-fold:

  1. Is there a better way to customize a parent template file than to copy it into the child theme and make changes?

  2. Is this actually a problem with a poorly structured theme? That is, should the theme writer have kept the function definition in the parent theme, taking account of possible child themes in use?

1

Is there a better way to customize a parent template file than to copy it into the child theme and make changes?

Not really. Some themes, like Genesis, are specifically designed for child theming, and instead of templates use action hooks to construct their pages. If the parent theme is built like this you can use remove_action() and add_action() to modify templates. However, this is something that the parent theme needs to be specifically designed to support. It's not applicable to most themes, and any implementation of this technique would be specific to the parent theme.

All I can suggest is to consult any documentation for the parent theme to see if they support any alternative methods for child themes.

Is this actually a problem with a poorly structured theme? That is, should the theme writer have kept the function definition in the parent theme, taking account of possible child themes in use?

When a developer creates a theme, they are also creating an API for child theme developers, whether they wanted to or not. APIs need to be stable. For the same reason WordPress can't just remove functions without some kind of fallback to prevent errors, theme developers shouldn't update their theme without consideration for child theme developers. So yes, I would argue that the theme developer should have kept the function definition.

The issue is that there's no way to enforce this. However, there's a couple of things you can do avoid this happening in the future.

  1. When using a child theme, don't update the parent theme without reviewing the changelog, or changes themselves, for potential issues.
  2. Code defensively. For example, whenever you use a function from a 3rd-party plugin or parent theme, check function_exists() first. If the parent theme removes a function then the output would disappear, but at least there's no fatal error.
1
  • 1
    After I wrote my post, I re-thought through the issue and came to the same conclusion. I appreciate your extensive comments. Besides teaching my students how code works, I'm teaching them to go to the community to be sure that their information is current. Your answer is an excellent example of the value of the community. Apr 30 '20 at 14:14

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.