Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active September 29, 2016 21:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Shelob9/7b5583e776d20deca75026fd459b7260 to your computer and use it in GitHub Desktop.
<?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' );
});
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' ] );
}
}
}
}
}
}
<?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