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 for your fast answer ! I did what you said and it worked, I don’t have the fatal error message anymore. But I have another problem : nothing happen to my website when I put my modified functions.php in my child theme. As if the parent theme still get over it. Do you know what could be the reason ?
    Here is my global functions.php (in my child theme), it’s really short : http://pastebin.com/3yDvA94n

    I also have another problem : I tried to apply your solution to the function I told you in my previous message I have difficulties with and it doesn’t worked.
    Here is what I have done : http://pastebin.com/bH3XGZxj
    And I get this message : “Fatal error: Cannot redeclare blackandwhite_widgets_init() (previously declared in /home/lemarketmc/www/wp-content/themes/blackandwhite-child/functions.php:19) in /home/lemarketmc/www/wp-content/themes/blackandwhite/functions.php on line 132”.
    I don’t understant why is it question of the parent theme ?

    I am sorry to bother you again but your help is really appreciated !

    KInd regards,
    Camille

  2. I’m not sure what’s happening with the CSS, it looks as if you’re calling your child theme’s style sheet correctly. I would probably check the path, perhaps hard code it for testing and see if the correct file is referenced. This may also help: http://codex.wordpress.org/Function_Reference/wp_enqueue_style

    As for the second problem, you’re re-declaring a function that appears to be present in the parent theme. Although you’re removing the hook, you cannot have two functions with the same name, even if they are in different files (just like with the earlier problem). In this case it’s blackandwhite_widgets_init(), which should probably be called new_blackandwhite_widgets_init() in your child theme.

    Hope this helps!

  3. Hi Camille, I had another idea about your style sheet: your child theme’s style.css is called automatically, there’s no need to import it via wp_enqueue_scripts() in your functions.php. If you want to import additional style sheets, I would probably do it via CSS using the @import statement.

  4. Hi Jay ! Thanks for your reply. Sorry I did not answer earlier but I was at work.

    In fact I just realized that my child theme is not working 🙁
    Wordpress recognized it, I have activated it but it is not working.
    All the php files I have modified and that I put in my child theme are working but my functions.php and my style.css which are actually not working ! I checked my wordpress backoffice and I just get blank page for functions.php and style.css.
    I guess that is why nothing is changing when I modify them.

    This is really weird cause my files functions.php and style.css in my ftp are written.
    I tried to cpoy paste them into my worpress backoffice but nothing happened.

    Do you know why ?
    you already have my fuctions.php (in my chil theme) : http://pastebin.com/3yDvA94n
    and here is the beginning of my css child theme : http://pastebin.com/Kgjk0sjj

    This is really weird cause my files functions.php and style.css in my ftp are written.
    I tried to copy paste them into my worpress backoffice but nothing happened.

    It seems to me that I did the right way but I don’t understand why wordpress recognize my child theme but don’t upload functions.php and syle.css.

    I read that the @import method was not recommanded, but that the wp_enqueue was the good method, that is why I tried the wp_enqueue method.

    I am deseperated ;(

Add your voice!

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