I was wondering whether it is possible to completely disable a function that is called in a parent theme's functions.php
and if so, how? (Of course without deleting the function. Rather by adding something to functions.php
of the child theme.)
2 Answers
Only under some circumstances.
If the parent theme's functions are wrapped in
function_exists
conditionals then you should be able to replace them. For example:// Parent if (!function_exists('p_wpse_95799')) { function p_wpse_95799() { // } }
If the child theme defines a function named
p_wpse_95799
then the parent function will not be used. If this is a possibility you'd just need to define a function that does nothing, much like the above.If the function is 'hooked' to some filter or action then you might be able to un-hook it. There is a catch. The child theme
functions.php
loads first which means you may won't be able to remove filters directly. You will have to hook them to run after the parent'sfunctions.php
has ran. Otherwise you will be trying to remove something that has not been added. From the Codex: "It is also worth noting that you may need to prioritise the removal of the filter to a hook that occurs after the filter is added. You cannot successfully remove the filter before it has been added." Mostly this will mean finding a hook that runs after the parentfunctions.php
and hooking to that, possibly manipulating the third parameter of add_action or add_filter to cause your function to run late.
Those are the cases I can think of right now. Perhaps I have forgotten a case. I trust someone will point that out if I have :)
There is no PHP level "remove function" and if you try to define two functions with the same name you will get an error.
As I can't see the code for the function you want to remove, I worry that you are going to break something by removing or altering it, if that is even possible. A little caution is in order.
While it's difficult to know exactly how to do what you want without seeing code, this may help - How to override parent functions in child themes?
Just ignore the override part of that Q&A if you're not interested in altering the function, and focus on how it goes about removing it.
Basically, you'll want to use use remove_action or remove_filter in your child theme's function.php
file and run it after the theme has been set up.
functions.php
file.