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