he Right Way to Override Theme Functions
Submitted by venutip on June 20th, 2009
File under
I started building my first WordPress site last week and almost immediately starting customizing the theme. Rather than editing the default theme directly, however, I created a child theme, basing it off one of the many theme frameworks available for WordPress. Creating the child theme was painless, and there is plenty of information on how to do so.
Pretty soon though, I got to the point where tweaking CSS rules wasn’t going to cut it: I needed to go deeper. I needed to change some of the functions in the parent theme. But while there was plenty of information on creating child themes, information on overriding theme functions was nowhere to be found. The closest I came was this helpful post.
This is an odd disconnect. How could there be so much information on one part of a best practice (creating a child theme), but basically none on how to make that best practice really work?
I can’t answer that question. But I do have my own version of how to override theme functions in WordPress – The Right Way.
The Child is the Father of the Man
(Bonus points if you get the reference.)
Here’s the basic outline for what I think is the Right Way To Do It:
- Copy (in full) the function you want to override from the parent theme.
- Paste it into
functions.php
in the root of your child theme’s folder. Iffunctions.php
doesn’t exist, create it. - Rename the function from
parent_theme_function
tochild_theme_function
. - Deactivate the parent function.
- Activate the child function.
The first three steps are self-explanatory, so I’ll just cover the last two steps.
Deactivate the Parent Function
There are actually two steps here. First, create a one-line function that removes the parent function from its “phase”. Then, add that one-line function to WordPress’ bootstrap phase (‘init’), so that the parent function actually gets removed.
Confused? Don’t worry. Here’s an example of how you would remove the
thematic_blogtitle()
function from the Thematic theme. Code comin’!// Remove the default Thematic blogtitle function
function remove_thematic_actions() {
remove_action('thematic_header','thematic_blogtitle',3);
}
// Call 'remove_thematic_actions' (above) during WP initialization
add_action('init','remove_thematic_actions');
In this case, we are removing the parent theme function (
thematic_blogtitle
) from thethematic_header
phase.
How do you know what the proper phase is? Look for a line like this in the parent function’s theme files:
add_action('phase','function', 'priority');
You can usually find this right after the function’s declaration. The line that adds
thematic_blogtitle
looks like this:add_action('thematic_header','thematic_blogtitle',3);
Note that you have to use the same priority that was used to add the function in the parent theme (in this case, 3) when you remove it. If no priority was used in the original
add_action
, you can skip it.Activate Your Child Function
To replace
thematic_blogtitle
, we just need to tell WordPress to call our child theme function in the place where it used to call the parent theme function. So, if our new function were calledfancy_theme_blogtitle
, we would add the following to functions.php
:add_action('thematic_header','fancy_theme_blogtitle', 3);
Save
functions.php
and voila! The code from fancy_theme_blogtitle
is run instead of the code from thematic_blogtitle
, and we didn’t have to hack the parent theme. This is crucial, because if there is ever an update to Thematic, we can get all the upgradey goodness without having to worry about how it will affect our child theme. Excellent!All Together Now
Putting all the code together:
// Removes thematic_blogtitle from the thematic_header phase
function remove_thematic_actions() {
remove_action('thematic_header','thematic_blogtitle',3);
}
// Call 'remove_thematic_actions' during WP initialization
add_action('init','remove_thematic_actions');
// Add our custom function to the 'thematic_header' phase
add_action('thematic_header','fancy_theme_blogtitle', 3);