Skip to content

Instantly share code, notes, and snippets.

@mklasen
Last active November 5, 2021 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklasen/96955d1c6e1774e37420b2e26ad4f283 to your computer and use it in GitHub Desktop.
Save mklasen/96955d1c6e1774e37420b2e26ad4f283 to your computer and use it in GitHub Desktop.
Remove fuzzy actions and filters in WordPress
<?php
class Main {
public function remove_fuzzy_hook( $hook_name, $callback, $priority ) {
global $wp_filter;
// Confirm the hook name exists.
if ( isset( $wp_filter[ $hook_name ] ) && is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) {
// Confirm the hook contains callbacks.
if ( property_exists( $wp_filter[ $hook_name ], 'callbacks' ) && is_array( $wp_filter[ $hook_name ]->callbacks ) ) {
// Confirm the priority exists and contains callbacks.
if ( isset( $wp_filter[ $hook_name ]->callbacks[ $priority ] ) && is_array( $wp_filter[ $hook_name ]->callbacks[ $priority ] ) ) {
// Loop over the callbacks.
foreach ( $wp_filter[ $hook_name ]->callbacks[ $priority ] as $function_key => $hook ) {
// If an array is provided, confirm that the class names match.
if ( is_array( $callback ) ) {
// If it's an array, make sure that the object names match.
if ( get_class( $hook['function'][0] ) !== $callback[0] ) {
continue; // No match.
}
$callback = $callback[1];
}
// Look for a match.
if ( isset( $hook['function'] ) && isset( $hook['function'][1] ) && $hook['function'][1] === $callback ) {
// Use the default WordPress system to remove the callback using the correct name.
remove_filter( $hook_name, $function_key, $priority );
}
}
}
}
}
}
}
<?php
class Fuzzy_Hooks_With_Instance {
private static $instance = null;
public static function getInstance() {
if ( self::$instance == null ) {
self::$instance = new Fuzzy_Hooks_With_Instance();
}
return self::$instance;
}
public function __construct() {
add_action( 'wp', array( $this, 'die_early' ), 5 );
}
public function die_early() {
die( 'Death by Fuzzy_Hooks_With_Instance' );
}
}
class Fuzzy_Hooks {
public function __construct() {
add_action( 'wp', array( $this, 'die_early' ), 5 );
}
public function die_early() {
die( 'Death by Fuzzy_Hooks' );
}
}
class Other_Class {
public function __construct() {
add_action( 'init', array( $this, 'prevent_die' ) );
add_action( 'wp', array( $this, 'die_anyway' ), 50 );
}
public function die_anyway() {
die('Natural death.');
}
public function prevent_die() {
// Does not work
remove_action( 'wp', 'die_early', 5 );
// Does not work.
remove_action( 'wp', array( 'Fuzzy_Hooks', 'die_early' ), 5 );
// Works when we can get an instance.
$fuzzy_hooks = Fuzzy_Hooks_With_Instance::getInstance();
remove_action( 'wp', array( $fuzzy_hooks, 'die_early' ), 5 );
// Always works.
$this->remove_fuzzy_hook( 'wp', array( 'Fuzzy_Hooks', 'die_early' ), 5 );
$this->remove_fuzzy_hook( 'wp', 'die_early', 5 );
}
public function remove_fuzzy_hook( $hook_name, $callback, $priority ) {
global $wp_filter;
// Confirm the hook name exists.
if ( isset( $wp_filter[ $hook_name ] ) && is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) {
// Confirm the hook contains callbacks.
if ( property_exists( $wp_filter[ $hook_name ], 'callbacks' ) && is_array( $wp_filter[ $hook_name ]->callbacks ) ) {
// Confirm the priority exists and contains callbacks.
if ( isset( $wp_filter[ $hook_name ]->callbacks[ $priority ] ) && is_array( $wp_filter[ $hook_name ]->callbacks[ $priority ] ) ) {
// Loop over the callbacks.
foreach ( $wp_filter[ $hook_name ]->callbacks[ $priority ] as $function_key => $hook ) {
// If an array is provided, confirm that the class names match.
if ( is_array( $callback ) ) {
// If it's an array, make sure that the object names match.
if ( get_class( $hook['function'][0] ) !== $callback[0] ) {
continue; // No match.
}
$callback = $callback[1];
}
// Look for a match.
if ( isset( $hook['function'] ) && isset( $hook['function'][1] ) && $hook['function'][1] === $callback ) {
// Use the default WordPress system to remove the callback using the correct name.
remove_filter( $hook_name, $function_key, $priority );
}
}
}
}
}
}
}
$fuzzy_hooks = Fuzzy_Hooks_With_Instance::getInstance();
new Other_Class();
new Fuzzy_Hooks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment