Created
November 25, 2016 06:20
-
-
Save PrafullaKumarSahu/c522c263389678f5ff27d1dfd368cb05 to your computer and use it in GitHub Desktop.
How to remove action hooking non static method and class can not be instantiated
This file contains hidden or 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
//http://stackoverflow.com/questions/40788732/how-to-remove-action-hooking-non-static-method-and-class-can-not-be-instantiated/40790864#40790864 | |
/** | |
* Do a hard unregister of an object's callback for the specified event name | |
* and priority level. | |
* | |
* In WordPress, the callback key (or unique ID) is generated using the hash ID of | |
* the object concatenated with the method name. In the event that you do not have | |
* the object itself, then we use this hard approach to first first the callback | |
* function and then do the remove. | |
* | |
* This process works for both filter and action events. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $event_name The name of the filter or action event | |
* @param integer $priority Priority level | |
* @param string $method_name Callback's method name | |
* | |
* @return void | |
*/ | |
function do_hard_unregister_object_callback( $event_name, $priority, $method_name ) { | |
$callback_function = get_object_callback_unique_id_from_registry( $event_name, $priority, $method_name ); | |
if ( ! $callback_function ) { | |
return false; | |
} | |
remove_filter( $event_name, $callback_function, $priority ); | |
} | |
/** | |
* Get the object's event registry unique ID for the given event name, priority | |
* level, and method name. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $event_name The name of the filter or action event | |
* @param integer $priority Priority level | |
* @param string $method_name Callback's method name | |
* | |
* @return string|boolean | |
*/ | |
function get_object_callback_unique_id_from_registry( $event_name, $priority, $method_name ) { | |
global $wp_filter; | |
if ( ! isset( $wp_filter[ $event_name ][ $priority ] ) ) { | |
return false; | |
} | |
echo '<pre>'; | |
var_dump( $wp_filter ); | |
echo '</pre>'; | |
foreach( $wp_filter[ $event_name ][ $priority ] as $callback_function => $registration ) { | |
if ( strpos( $callback_function, $method_name, 32) !== false) { | |
return $callback_function; | |
} | |
} | |
return false; | |
} | |
add_action( 'plugins_loaded', 'remove_woocommerce_add_sold_by_single_callback', 1 ); | |
/** | |
* Unregister the WooCommerce `WC_Product_Vendors_Vendor_Frontend::add_sold_by_single` from the | |
* event `woocommerce_single_product_summary`. | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
function remove_woocommerce_add_sold_by_single_callback() { | |
global $wp_filter; | |
var_dump( $wp_filter['woocommerce_single_product_summary'][39] ); | |
do_hard_unregister_object_callback( 'woocommerce_single_product_summary', 39, 'add_sold_by_single'); | |
if ( ! isset( $wp_filter['woocommerce_single_product_summary'][39] ) ) { | |
var_dump( $wp_filter['woocommerce_single_product_summary'] ); | |
} else { | |
var_dump( $wp_filter['woocommerce_single_product_summary'][39] ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment