I was wondering why isn’t anyone (haven’t be able to find one with it) adding custom hooks an the beginning and at the end of the theme’s function.php
for theme development.
The reason I’m asking this is while making a child theme, I had to overwrite some default setups like image sizes set in the parent theme’s main function.php
(no !function_exists
to overwrite on). So anything I write in my child’s function.php
will be overwritten.
Aside from of course adding the function_exists
wrapper, the solution I had in mind was just adding a do_action()
at the beginning and at the end of the function.php. But is this a bad practice? If so, why?
I am unable to find any answers or anyone asking this anywhere.
Code in a
functions.php
(or any file loaded by that file) should not run automatically, eg. when the file is included. All code should be bound to hooks.That means, you donât need a special action in the parent theme. The child theme can turn off all parent theme callbacks with
remove_action()
andremove_filter()
.But you can make that easier for child theme authors by adding a custom action after you have registered the callbacks.
Example
Parent theme
Child theme
See also this similar example for outdated PHP versions.
If your parent theme ignores the principle of separation of concerns and mixes everything in just one or two setup functions, use a later priority and overwrite those calls:
Parent theme
Child theme