How to override a parent function from your Child Theme

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:

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:

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

 

Further Reading

  • http://wordpress.stackexchange.com/questions/7557/how-to-override-parent-functions-in-child-themes
  • http://codex.wordpress.org/Child_Themes
  • http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

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!!!!!

    1. 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!

  2. 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

    1. 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

      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

        1. 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!

        2. 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.

        3. 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 ;(

    1. Hi Camille, it looks like your functions.php file is working just fine, because you’re pulling in the style sheet via wp_enqueue_scripts() – which is part of functions.php. Otherwise style.css wouldn’t be loaded. I’m guessing it’s the way you call hooks and actions. However I can’t help you with fixing those details – sorry! To troubleshoot, try something simple first before getting too advanced with your coding. Here’s an example:

      If you see the test message above the footer, you know your child theme is working in principle. See if your other functions work in principle with such a test first, then add the functionality you need.

      Good luck!

      1. Hi Jay !
        Thank you very much for your reply. You are right, the way I call hooks and actions is wrong. When you are not an expert in code that is quite difficult to deal with, and just to know where the error come from is a big step forward !
        I am gonna work on it and when it will finally work I will show you the results !
        Thanks again for your help ! and for your patience 🙂

Add your voice!