Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Add 2 PHP functions for WP Filter API, it's allow to remove filter/action on hoo

## Remove filters with method name only

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

Allow to remove method for an hook when, it's a class method used and class don't have global for instanciation !

## Remove filters with class and method name

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

Allow to remove method for an hook when, it's a class method used and class don't have global for instanciation, but you know the class name :)

Expand All @@ -25,7 +25,7 @@ Allow to remove method for an hook when, it's a class method used and class don'
// First sample
class MyClassA {
function __construct() {
add_action( 'wp_footer', array( $this, 'my_action' ), 10 );
add_action( 'wp_footer', array( $this, 'my_action' ) );
}
function my_action() {
print '<h1>' . __class__ . ' - ' . __function__ . '</h1>';
Expand All @@ -36,7 +36,7 @@ new MyClassA();
// Second sample
class MyClassB {
function __construct() {
add_action( 'wp_footer', array( $this, 'my_action' ), 10 );
add_action( 'wp_footer', array( $this, 'my_action' ) );
}
function my_action() {
print '<h1>' . __class__ . ' - ' . __function__ . '</h1>';
Expand All @@ -55,7 +55,7 @@ The right way to load class is : `$my_class_b = new MyClassB();`
This first method, `remove_filters_with_method_name();`, is fairly aggressive because it removes all filters that have the name "my_action", whatever the class.

```php
remove_filters_with_method_name( 'wp_footer', 'my_action', 10 );
remove_filters_with_method_name( 'wp_footer', 'my_action' );
```

This call will remove the 2 filters for classes MyClassA and MyClassB.
Expand All @@ -65,11 +65,11 @@ This call will remove the 2 filters for classes MyClassA and MyClassB.
The second method, `remove_filters_for_anonymous_class();`, is more accurate because it is necessary to specify both the name of the method but also the name of the PHP class.

```php
remove_filters_for_anonymous_class( 'wp_footer', 'MyClassB', my_action', 10 );
remove_filters_for_anonymous_class( 'wp_footer', 'MyClassB', 'my_action' );
```

This call will only remove the filter for the class MyClassB.

## Tips

The priority argument of the two functions is important, you must use the same priority as the hook you want to remove !
The priority argument of the two functions is important, you must use the same priority as the hook you want to remove !
6 changes: 3 additions & 3 deletions wp-filters-extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* Allow to remove method for an hook when, it's a class method used and class don't have global for instanciation !
*/
function remove_filters_with_method_name( $hook_name = '', $method_name = '', $priority = 0 ) {
function remove_filters_with_method_name( $hook_name = '', $method_name = '', $priority = 10 ) {
global $wp_filter;

// Take only filters on right hook name and priority
Expand All @@ -51,7 +51,7 @@ function remove_filters_with_method_name( $hook_name = '', $method_name = '', $p
/**
* Allow to remove method for an hook when, it's a class method used and class don't have variable, but you know the class name :)
*/
function remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $method_name = '', $priority = 0 ) {
function remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $method_name = '', $priority = 10 ) {
global $wp_filter;

// Take only filters on right hook name and priority
Expand All @@ -71,4 +71,4 @@ function remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $
}

return false;
}
}