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.phpin the root of your child theme’s folder. Iffunctions.phpdoesn’t exist, create it. - Rename the function from
parent_theme_functiontochild_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 the thematic_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 called fancy_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);
Know a Better Way?
As I stated previously, I’ve only been using WordPress for about a week. If you know a better way to do this, by all means, please let me know!
Awesome!!!
Thanks so much! I just ran into exactly this problem and was just about to give up and hack the Thematic functions up.
Your fix saved me some headaches. I’ll be trying it out now…
modify functions.php
I’m trying to separate the parent of the child theme, I try to modify functions.php (hybrid theme, and child theme), and doing it alone.
Hybrid theme is the parent, Critical theme is the child, i try to make one but with the characteristics of the child,
is this possible?
dsf
You can join the theme club for free or pay a minimal yearly fee to get help and in-depth tutorials on customizing your site.
Post new comment