Created
September 5, 2019 07:00
-
-
Save kagg-design/7cbda38a3ce76c0502748f8ddbc9bebe to your computer and use it in GitHub Desktop.
Remove current action or filter from plugin
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 | |
/** | |
* Remove current action or filter from plugin | |
* | |
* @param string $class_name Class name enqueueing the action. | |
* @param null $action_name Action name. | |
*/ | |
function remove_plugin_action( $class_name, $action_name = null ) { | |
global $wp_filter; | |
if ( null === $action_name ) { | |
$action_name = current_action(); | |
} | |
/** @var WP_Hook $hooks WP hooks. */ | |
$hooks = $wp_filter[ $action_name ]; | |
$callbacks = $hooks->callbacks; | |
foreach ( $callbacks as $priority => $actions ) { | |
foreach ( $actions as $action ) { | |
$function = $action['function']; | |
if ( is_array( $function ) ) { | |
$callback = $function[0]; | |
if ( | |
( is_object( $callback ) && get_class( $callback ) === $class_name ) || | |
( is_string( $callback ) && $callback === $class_name ) | |
) { | |
remove_action( $action_name, $function, $priority ); | |
} | |
} | |
} | |
} | |
} | |
// Example of usage. | |
/** | |
* Enqueue Google maps scripts only on certain pages. | |
*/ | |
function action_google_maps_script() { | |
if ( ! ( is_page_template( 'templ-contact.php' ) ) ) { | |
remove_plugin_action( 'PageSpeed_Optimization' ); | |
} | |
} | |
add_action( 'wp_enqueue_scripts', 'action_google_maps_script', 0 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment