How to override a parent function from your Child Theme

- by

When you’re writing a Child Theme you can easily add your own functions to a custom functions.php file. But sometimes you want to replace or remove a function that the original theme provides.

Because the Child Theme’s functions.php file is loaded BEFORE the parent’s file, you can’t just define the same function again because it would be overwritten. You also can’t rely on the original theme to provide a “pluggable” parent function, as suggested in the WordPress Codex.

What you can do however is to remove the parent’s function’s hook, and then re-hook your own function in its place. Let’s look at how to do this.

 

Removing the Parent Function

In this example the parent function has setup some styles which we don’t want, originally called via

add_action( ‘wp_enqueue_scripts’, ‘p2_iphone_style’, 1000 );

Let’s undo this:

// remove parent function
function my_theme_setup () {
	remove_action( 'wp_enqueue_scripts', 'p2_iphone_style', 1000 );
}
add_action( 'after_setup_theme', 'my_theme_setup' );

Note that you’ll have to provide all parameters from the original hook.

 

Replace the Parent Function with your own

If you want to do something else instead, define it with another function:

// replace parent function
function my_addition () {
	// your code goes here
}

function my_theme_setup () {
	remove_action( 'wp_enqueue_scripts', 'p2_iphone_style', 1000 );
	add_action( 'wp_enqueue_scripts', 'my_addition', 1000 );
}
add_action( 'after_setup_theme', 'my_theme_setup' );

First we remove the original action, then we add the same action again, plugging in our child theme’s function.

 

Further Reading



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site witout any pesky ads! More details here.

12 thoughts on “How to override a parent function from your Child Theme”

  1. Thank you so much! I have search and tried many other and similar solutions for overriding Parent Functions. This is the first one that was presented in simple understandable way, so that my virgin php mind could implement. YOU ROCK!!!!!

  2. That is such a lovely thing to say, thank you! I’m so glad it helped you out. I remember this was driving me nuts too, especially when you’re not coding PHP on a regular basis.

    All the best from Miami Beach!

  3. Hi thank you very much for the tip 😉

    I created a child theme to avoid loosing all the modifications I have done on my theme (Black and White – Modern Theme). In functions.php I have to put only the functions I have modified.
    I have only 3 functions I want to add in my child theme, these are the only ones I have modified in comparison to my parent theme.

    I tried to implement the function remove_parent_functions() on my child theme, in functions.php.
    It worked for the first one, but when I did it again for the second one I get this message : ”
    “Fatal error: Cannot redeclare remove_parent_functions() (previously declared in /home/lemarketmc/www/wp-content/themes/blackandwhite-child/functions.php:24) in /home/lemarketmc/www/wp-content/themes/blackandwhite-child/functions.php on line 49”.

    Please could you help me ?
    My code is available here : http://pastebin.com/tSCbFj7P
    On lines 2 to 26 everything is alright, but when I add lines 28 to 38 then I get the fatal error message.

    By the way I have anoter element to modify in the functions.php of my child theme but I just don’t manage to integrate function remove_parent_functions(). I guess I do it the wrong way, but I don’t know where are my mistakes.
    Here is the code to modify : http://pastebin.com/Mdq0Jg7e

    If you also culd help me on this, it would be great.

    Thank you so much in advance !
    Kind regards,
    Camille

  4. Hello Camille,

    you’re in luck, that’s an easy fix: in PHP, function names need to be unique. You can’t declare the same function twice (in your case, remove_parent_functions). Either add all the code into a single function, or call the second function something else (for example, remove_parent_functions_2). That should do the trick.

    All the best,

    JAY

Add your voice!

This site uses Akismet to reduce spam. Learn how your comment data is processed.