Skip to main content

The Right Way to Override Theme Functions

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:

  1. Copy (in full) the function you want to override from the parent theme.
  2. Paste it into functions.php in the root of your child theme’s folder. If functions.php doesn’t exist, create it.
  3. Rename the function from parent_theme_function to child_theme_function.
  4. Deactivate the parent function.
  5. 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.

Wow

After burning a lot of time looking for a way to custom replace a footer display function without touching the update-prone php code of the parent theme, I thankfully found this page and gave its info a try in a WP 3.0 child theme. It worked beautifully! Thank you.

A small modification

This works great for some, but not all themes because in some cases the parent functions.php contains functions that kick in too quickly. For example if you were to try to replace twentyten_widgets_init() you would not be able to with the script above. That’s because the child functions.php is called before the parent functions.php, so even if you unhook the function it gets rehooked later on. To fix this change init to after_setup_theme in the remove action call like this:

// Call 'remove_thematic_actions' during WP initialization
add_action('after_setup_theme','remove_thematic_actions');

I found this tip at http://ottopress.com/2010/wordpress-protip-child-themes/

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h1> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options