4

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

1
  • 2
    This question cannot reasonably be answered as-asked. Please post relevant code: in this case, the actual function definition of the function in the parent Theme functions.php file. Apr 14, 2013 at 21:58

2 Answers 2

8

Only under some circumstances.

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

  2. 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's functions.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 parent functions.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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.