If you are a budding WordPress theme developer there is one thing you will have to end up knowing sooner or later. That is, how to override parent theme functions in WordPress.
The reason you would want to do this is to be able to alter some or most of the behavior of a parent theme while retaining other features that are inherited into your child theme.
With this WordPress feature, three main parts of the parent theme can be manipulated. They are the templates, the main CSS stylesheet, and the functions.
1. The templates: These are replaced by those in the child theme if they bear the same name and location. This is necessary to retain the well-formed output which normally ends up in your web browser as HTML.
2. CSS Style: The style.css file of your child theme, and any others by extension, are appended below the ones from the parent theme. The browser will then process the CSS giving priority to the last occurrence of any matching rules.
3. Functions: The functions.php file contains functions specific to your theme. unlike templates or CSS styles, you cannot override parent theme functions simply by declaring the same function in the child theme.
The parent theme functions are registered after those of the child theme which means any similar function names will be overridden by those of the parent theme in WordPress.
So how do I Override Parent Theme Functions?
Because the functions in WordPress are first registered before used, it allows you to specify which function ends up being used on your website. There are three main ways you can do this:
- Use pluggable functions
- Change function priority
- Remove functions from the hook that they are attached to
Using Pluggable Functions
You can use PHP’s A chip in a computer is electronic circuits or an integrated circuit (also referred to as an IC or a microchip) embedded on a flat piece of semiconductor material that is normally silicon. Integrated circuits can be analog, digital, or mixed-signal. The minute size of computer chips brings the advantage of allowing high speed, low power dissipation, and reduced manufacturing... More function
function_exists() to check if the function has already been declared. If it has not been declared then the function will be declared and used.
This method is the best practice for those developing parent themes and will stop the parent theme function from being declared if it has been declared in the child theme.
Change Function Priority
It is not always the case that you encounter a parent theme developed with pluggable functions, which in itself is a bad design for a parent theme.
WordPress provides another solution to this problem. Basically in WordPress functions are generally added to an action or filter hook. When this is done you may also supply a priority level to it.
If you do not supply a priority it is given the default of 10. If you find the parent theme function has no priority assigned then anything above 10 should give it a higher priority as in WordPress functions are added to the filter hook in ascending order.
Here is an example of how this works in your child theme.
Remove Functions From the Hook
Another alternative is to completely remove the parent theme function from the filter hook and add your child theme in its place. This option can also be used to remove a function from the filter hook and net necessarily replace it if you do not need an alternative.
This can be done using the WordPress functions
remove_filter() depending on whether the function was added to an action or a filter hook in the parent theme.
This is how you can remove a function from an action or a filter hook.
remove_filter() functions must be attached to a filter hook for it to work and that is why it is not called stand-alone.
You will also need to specify the priority of the original function and set the same priority level or lower because technically you cannot remove an action before it has been triggered.
At this point, the parent function is no longer registered and you are now free to declare it in your child theme as shown above.
Hopefully, this guide should point you on your way on how to override parent theme functions in WordPress.