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!

hi, is there a possibility to

hi, is there a possibility to override this function on my parent theme?

function load_theme_libaries() {
require_once (MTHEME_FUNCTIONS . ‘scripts-styles-register.php’);
require_once (MTHEME_FUNCTIONS . ‘common-scripts.php’);
require_once (MTHEME_FUNCTIONS . ‘widgetize-theme.php’);
require_once (MTHEME_FUNCTIONS . ‘custom-post-types.php’);
require_once (MTHEME_FUNCTIONS . ‘custom-post-sorter.php’);
 }

this function is called in same parent functions.php files with a simple line of code:

load_theme_libaries();

Success!!

Thanks for sharing this article. I was struggling to override a function in twentyfourteen/inc/widgets.php so that I could use one in twentyfourteen_child/inc/widgets.php.

Until I read your example I didn’t realise I had to ‘set up’ my code to remove the parent function in an earlier phase / hook.

E.G. The parent function is added in when the widgets_init hook runs;

add_action( ‘widgets_init’, ‘twentyfourteen_widgets_init’ );

But my code to remove the parent function was not successful until I stopped using the widgets_init hook and used the earlier init hook.

This code is in my child themes functions.php file.

function twentyfourteen_child_widgets_init() {
// Function to override parent function twentyfourteen_widgets_init()
Some code to register sidbebars…..
 }

function remove_twentyfourteen_widgets_init() {
remove_action(‘widgets_init’, ‘twentyfourteen_widgets_init’);
}
add_action( ‘init’, ‘remove_twentyfourteen_widgets_init’); // Notice we are adding our function at the init phase

add_action( ‘widgets_init’, ‘twentyfourteen_child_widgets_init’ );

It might not be perfect but it works! :)

IGNORE PREVIOUS POST !

Sorry, it has stopped working, please ignore previous post :(

Thanks for this walk through.

Thanks for this walk through. It helped :).

Thank you so much :)

Just chipping in to say thank you, even 6 years later, your post is still helping people out :D

Child Theme Image Sizer Function Override

You explained very well but in my case the function that I want to override is Image resizing action.

if ( ! function_exists( 'alx_setup' ) ) {
    
    function alx_setup() {      
        // Thumbnail sizes
        add_image_size( 'thumb-small', 80, 80, true );
        add_image_size( 'thumb-standard', 200, 200, true );
        add_image_size( 'thumb-large', 620, 300, true );
    }
    
}
add_action( 'after_setup_theme', 'alx_setup' );

I want to change the thumbnail image sizes in my child theme. Any Help!

Great tutorial

Thank you for great and simple tutorial.
Works like a charm.

Is there some way to output a

Is there some way to output a list of all actions / filters that are being processed by WP for debugging purposes? Basically the whole filter/action structure of Wordpress works perfect, but sometimes I feel kind of lost and would like an overview of all actions/filters/hooks.

Theme Saver!

What a quality article! Certainly solved my child-parent thematic problem/s…Many thanks.

problem with child theme

right after creating the functions.php in my child theme directory, when I go to see website, automatically it adds some codes to end of functions.php in child theme and causes this error:
“?>
Fatal error: Cannot redeclare _check_active_widget() (previously declared in C:\xampp\htdocs\GA\wp-content\themes\ga-lander\functions.php:6) in C:\xampp\htdocs\GA\wp-content\themes\goodnews\functions.php on line 76”
and when I look in functions.php, I see there has been added 430 lines of code to it.
please tell me how to fix it.
thank you.

Thanks for the post!

Hey friend thanks so much for the post! Actually i work with Divi Theme i thinks thats it’s easy to work with this kind of themes.

Comment viewing options

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