Email Signature_logo

REMOVE ACTIONS AND FILTERS VIA EXTERNAL CLASSES

You already voted!
remove actons and filters

Table of Contents

REMOVE FILTER

WordPress adds a hash (unique identifier) to the function name and saves it in the global $wp filter variable. This can be used to remove default functions from a filter hook and substitute them with anything else. The $callback and $priority arguments must correspond when the hook was added to delete it. This applies to filters as well as actions. If the removal fails, there will be no notice. As a result, using the remove_filter function has no effect.

Example of the function to use:

function my_remove_filter($tag, $function_name, $priority = 10){
    global $wp_filter;
    if( isset($wp_filter[$tag]->callbacks[$priority]) and !empty($wp_filter[$tag]->callbacks[$priority]) ){
        $wp_filter[$tag]->callbacks[$priority] = array_filter($wp_filter[$tag]->callbacks[$priority], function($v, $k) use ($function_name){
            return ( stripos($k, $function_name) === false );
        }, ARRAY_FILTER_USE_BOTH );
    }
}

SIMPLE PHP METHOD

WP Filter API allows you to remove filter/action on hooks for specified instances with CLASS PHP by utilizing simply two PHP methods.

Remove just the filters containing the method name:

remove_filters_with_method_name( $hook_name = '', $method_name = '', $priority = 0 );

The above method allows you to delete a hook’s method if it’s a class method.

Removing filters with class and method name:

remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $method_name = '', $priority = 0 );

Allows you to delete a hook method when it is a class method and the class does not have a global for insatiation, but you know the class name.

USE A PLUGIN

A common pattern present in many plugins is something similar to the “Singleton” pattern.
They build a getInstance() function to retrieve the class’s sole instance.
This is the most effective solution I’ve seen.

Plugin example:

class ExamplePlugin
{
    protected static $instance = NULL;

    public static function getInstance() {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }
}

The first time getInstance() is invoked, the class is instantiated and its pointer is saved.
You can utilize this to entice people to take action.

One issue is that if you use such a thing, you can’t use getInstance() inside the constructor.
This is because the constructor is called before the $instance is set, therefore invoking getInstance() from the constructor causes an infinite loop, which destroys everything.

One option is to explicitly have a “init” function in the class to set up your actions and whatnot, rather than using the constructor (or, at the very least, not using getInstance() within it).


As an example:

public static function init() {
    add_action( 'wp_footer', array( ExamplePlugin::getInstance(), 'my_action' ) );
}

Instantiating the plugin becomes as simple as this with something like this at the end of the file, once the class has been defined and such:

ExamplePlugin::init();

Init begins adding your actions and calls getInstance() to instantiate the class and ensure that only one of them exists.


If you don’t have an init function, you can use this to create the class from scratch:

ExamplePlugin::getInstance();

Removing the action hook from the outside via another plugin:

remove_action( 'wp_footer', array( ExamplePlugin::getInstance(), 'my_action' ) );

Once hooked to the plugins_loaded action hook, it will undo the action hooked up by the original plugin.

#Customer Speak

Join Mathiti Ventures AS a Senior Project Manager!

We’re growing and looking for talented people like you!

We’re not big on resumes. They can hold back great talent. Instead, we focus on the content of the person, not the paper

So, we’ve crafted a roughly 30-minute test about the things that are important for this position. Taking this will help us both discover our potential fit.

 

"*" indicates required fields

Max. file size: 5 MB.
Max. file size: 5 MB.

Join Mathiti Ventures AS a Senior WordPress Developer!

We’re growing and looking for talented people like you!

We’re not big on resumes. They can hold back great talent. Instead, we focus on the content of the person, not the paper

So, we’ve crafted a roughly 30-minute test about the things that are important for this position. Taking this will help us both discover our potential fit.

 

"*" indicates required fields

Max. file size: 5 MB.
Max. file size: 5 MB.

Join Mathiti Ventures AS a Senior PHP Developer!

We’re growing and looking for talented people like you!

We’re not big on resumes. They can hold back great talent. Instead, we focus on the content of the person, not the paper

So, we’ve crafted a roughly 30-minute test about the things that are important for this position. Taking this will help us both discover our potential fit.

 

"*" indicates required fields

Max. file size: 5 MB.
Max. file size: 5 MB.

Join Mathiti Ventures as a Senior React Developer!

We’re growing and looking for talented people like you!

We’re not big on resumes. They can hold back great talent. Instead, we focus on the content of the person, not the paper

So, we’ve crafted a roughly 30-minute test about the things that are important for this position. Taking this will help us both discover our potential fit.

 

"*" indicates required fields

Max. file size: 5 MB.
Max. file size: 5 MB.

Join Mathiti Ventures as a Job Title!

We’re growing and looking for talented people like you!

We’re not big on resumes. They can hold back great talent. Instead, we focus on the content of the person, not the paper

So, we’ve crafted a roughly 15-minute test about the things that are important for this position. Taking this will help us both discover our potential fit.

 

 

Coming Soon