Skip to content

Instantly share code, notes, and snippets.

@kagg-design
Created September 5, 2019 07:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kagg-design/7cbda38a3ce76c0502748f8ddbc9bebe to your computer and use it in GitHub Desktop.
Save kagg-design/7cbda38a3ce76c0502748f8ddbc9bebe to your computer and use it in GitHub Desktop.
Remove current action or filter from plugin
<?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