Last active
September 29, 2016 21:52
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_footer', function(){ | |
$x = 1; | |
} ); | |
class class_test { | |
public function __construct() { | |
add_action( 'wp_footer', [ $this, 'callback' ] ); | |
} | |
public function callback(){ | |
$x = 1; | |
} | |
} | |
class static_test { | |
public static function callback(){ | |
$x = 1; | |
} | |
} | |
add_action( 'wp_head', function(){ | |
new class_test(); | |
add_action( 'wp_footer', [ 'static_test', 'callback' ], 25 ); | |
$remover = new Remover(); | |
$remover->remove_all( 'wp_footer' ); | |
$remover->re_add( 'wp_footer' ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Remover { | |
/** | |
* Track removed hooks | |
* | |
* @var array | |
*/ | |
protected $removed = []; | |
/** | |
* Remove all callbacks on a hook with ability to add back later | |
* | |
* @param string $hook Hook name | |
*/ | |
public function remove_all( $hook ){ | |
global $wp_filter; | |
if ( isset( $wp_filter[ $hook ] ) ) { | |
if ( class_exists( 'WP_Hook' ) ) { | |
$all = clone $wp_filter[ $hook ]; | |
}else{ | |
$all = $wp_filter[ $hook ]; | |
} | |
if( ! empty( $all ) ){ | |
$this->removed[ $hook ] = $all; | |
remove_all_filters( $hook ); | |
} | |
} | |
} | |
/** | |
* Add back all callbacks removed from a hook | |
* | |
* Must have used $this->remove_all() to remove | |
* | |
* @param string $hook Hook name | |
*/ | |
public function re_add( $hook ){ | |
if( isset( $this->removed[ $hook ] ) ){ | |
$hooked = $this->removed[ $hook ]; | |
if ( ! empty( $hooked ) ) { | |
if ( class_exists( 'WP_Hook' ) ) { | |
$priorities = $hooked->callbacks; | |
} else { | |
$priorities = $hooked; | |
} | |
foreach ( $priorities as $priority => $callbacks ) { | |
foreach ( $callbacks as $callback ) { | |
add_filter( $hook, $callback[ 'function' ], $priority, $callback[ 'accepted_args' ] ); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Remover { | |
/** | |
* Track removed hooks | |
* | |
* @var array | |
*/ | |
protected static $removed = []; | |
/** | |
* Remove all callbacks on a hook with ability to add back later | |
* | |
* @param string $hook Hook name | |
*/ | |
public static function remove_all( $hook ){ | |
global $wp_filter; | |
if ( isset( $wp_filter[ $hook ] ) ) { | |
if ( class_exists( 'WP_Hook' ) ) { | |
$all = clone $wp_filter[ $hook ]; | |
}else{ | |
$all = $wp_filter[ $hook ]; | |
} | |
if( ! empty( $all ) ){ | |
self::$removed[ $hook ] = $all; | |
remove_all_filters( $hook ); | |
} | |
} | |
} | |
/** | |
* Add back all callbacks removed from a hook | |
* | |
* Must have used $this->remove_all() to remove | |
* | |
* @param string $hook Hook name | |
*/ | |
public static function re_add( $hook ){ | |
if( isset( self::$removed[ $hook ] ) ){ | |
$hooked = self::$removed[ $hook ]; | |
if ( ! empty( $hooked ) ) { | |
if ( class_exists( 'WP_Hook' ) ) { | |
$priorities = $hooked->callbacks; | |
} else { | |
$priorities = $hooked; | |
} | |
foreach ( $priorities as $priority => $callbacks ) { | |
foreach ( $callbacks as $callback ) { | |
add_filter( $hook, $callback[ 'function' ], $priority, $callback[ 'accepted_args' ] ); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment