1

Is there a way to remove action from theme that are added via plugin's class inside the constructor? Example:

class WP_Travel_Engine_Enquiry_Form_Shortcodes
{

    function __construct()
    {
        add_action('wp_travel_engine_enquiry_form', array( $this, 'wpte_enquiry_form' ));
    }
}
new WP_Travel_Engine_Enquiry_Form_Shortcodes;

So can I remove the action wpte_enquiry_form from wp_travel_engine_enquiry_form via theme? I am unable to do so as the action is added instantly when the object is created. Is there a way possible?

Thanks for your time!

0

If that class works as a singleton and has a getInstance() method (or similar) to retrieve the instance, you can remove the action pretty much the same way it was added:

remove_action('wp_travel_engine_enquiry_form', array( $instance, 'wpte_enquiry_form' ));

Edit: I've looked into this some more, and you find the necessary information by looking into $wp_filters (global variable, so use global $wp_filters; before working with it in a function).

add_action("init", function() {
    global $wp_filter;
    foreach($wp_filter["wp_travel_engine_enquiry_form"][10] as $id => $filter) {
    if(is_array($filter["function"]) && && count($filter["function"]) == 2 &&
        get_class($filter["function"][0]) == "WP_Travel_Engine_Enquiry_Form_Shortcodes" &&
        $filter["function"][1] == "wpte_enquiry_form") {
            remove_action("wp_travel_engine_enquiry_form", $id);
        }
    }
}, 99);

This code is looking at the filters/actions (they are the same thing, but filters are generally expected to return something; internally, add_action is an alias for add_filter) for the wp_travel_engine_enquiry_form hook. 10 is the default priority, but you could easily iterate of the other priorities as well.

It then looks at each individual filter whether it is a callable with an object and a method, and whether that object is of the right type and the right method we want to remove. As the ID is known then, remove_action is used to manipulate $wp_filter.

It's not the cleanest of ways, but drastic times call for drastic measures.

  • As you can see, no object is defined in the plugin class. So, how can I use remove action in the theme to remove that action. Is there any other way around? Please help! – saurav.rox Jan 18 '18 at 6:14
  • @saurav.rox I've edited my answer and added a way to solve this. – janh Jan 18 '18 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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