Is it possible to disable a function of a parent theme?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x