Một ví dụ quan trọng Remove action from a plugin class (ok)
https://wordpress.stackexchange.com/questions/304859/remove-action-from-a-plugin-class
Ví dụ 1:
C:\xampp\htdocs\testcode\wp-content\themes\aveo\functions.php
function remove_filters_with_method_name( $hook_name = '', $method_name = '', $priority = 0 ) {
global $wp_filter;
// Take only filters on right hook name and priority
if ( ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) {
return false;
}
// Loop on filters registered
foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) {
// Test if filter is an array ! (always for class/method)
if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
// Test if object is a class and method is equal to param !
if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && $filter_array['function'][1] == $method_name ) {
// Test for WordPress >= 4.7 WP_Hook class (https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/)
if ( is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) {
unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $unique_id ] );
} else {
unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] );
}
}
}
}
return false;
}
remove_filters_with_method_name( 'wpforms_process_entry_save', 'entry_save', 10 );
C:\xampp\htdocs\testcode\wp-content\plugins\wpforms\includes\class-process.php
public function entry_save( $fields, $entry, $form_id, $form_data = array() ) {
do_action( 'wpforms_process_entry_save', $fields, $entry, $form_id, $form_data );
return $this->entry_id;
}
C:\xampp\htdocs\testcode\wp-content\plugins\wpforms\pro\wpforms-pro.php
public function init() {
add_action( 'init', array( $this, 'load_textdomain' ), 10 );
add_filter( 'plugin_action_links_' . plugin_basename( WPFORMS_PLUGIN_DIR . 'wpforms.php' ), array( $this, 'plugin_action_links' ), 11 );
add_action( 'wpforms_loaded', array( $this, 'objects' ), 1 );
add_action( 'wpforms_loaded', array( $this, 'updater' ), 30 );
add_action( 'wpforms_install', array( $this, 'install' ), 10 );
add_filter( 'wpforms_settings_tabs', array( $this, 'register_settings_tabs' ), 5, 1 );
add_filter( 'wpforms_settings_defaults', array( $this, 'register_settings_fields' ), 5, 1 );
add_action( 'wpforms_settings_init', array( $this, 'reinstall_custom_tables' ) );
add_action( 'wpforms_process_entry_save', array( $this, 'entry_save' ), 10, 4 );
add_action( 'wpforms_form_settings_general', array( $this, 'form_settings_general' ), 10 );
add_filter( 'wpforms_overview_table_columns', array( $this, 'form_table_columns' ), 10, 1 );
add_filter( 'wpforms_overview_table_column_value', array( $this, 'form_table_columns_value' ), 10, 3 );
add_action( 'wpforms_form_settings_notifications', array( $this, 'form_settings_notifications' ), 8, 1 );
add_action( 'wpforms_form_settings_confirmations', array( $this, 'form_settings_confirmations' ) );
add_filter( 'wpforms_builder_strings', array( $this, 'form_builder_strings' ), 10, 2 );
add_filter( 'wpforms_frontend_strings', array( $this, 'frontend_strings' ) );
add_action( 'admin_notices', array( $this, 'conditional_logic_addon_notice' ) );
add_action( 'wpforms_builder_print_footer_scripts', array( $this, 'builder_templates' ) );
add_filter( 'wpforms_email_footer_text', array( $this, 'form_notification_footer' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueues' ) );
add_filter( 'wpforms_helpers_templates_get_theme_template_paths', array( $this, 'add_templates' ) );
add_filter( 'wpforms_integrations_usagetracking_is_enabled', '__return_true' );
$this->allow_wp_auto_update_plugins();
}
Ví dụ 2: https://qastack.vn/wordpress/36013/remove-action-or-remove-filter-with-external-classes
if ( ! function_exists ( 'remove_class_filter' ) ) {
/**
* Remove Class Filter Without Access to Class Object
*
* In order to use the core WordPress remove_filter() on a filter added with the callback
* to a class, you either have to have access to that class object, or it has to be a call
* to a static method. This method allows you to remove filters with a callback to a class
* you don't have access to.
*
* Works with WordPress 1.2 - 4.7+
*
* @param string $tag Filter to remove
* @param string $class_name Class name for the filter's callback
* @param string $method_name Method name for the filter's callback
* @param int $priority Priority of the filter (default 10)
*
* @return bool Whether the function is removed.
*/
function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
global $wp_filter;
// Check that filter actually exists first
if ( ! isset( $wp_filter[ $tag ] ) ) return FALSE;
/**
* If filter config is an object, means we're using WordPress 4.7+ and the config is no longer
* a simple array, rather it is an object that implements the ArrayAccess interface.
*
* To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated)
*
* @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
*/
if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
$callbacks = &$wp_filter[ $tag ]->callbacks;
} else {
$callbacks = &$wp_filter[ $tag ];
}
// Exit if there aren't any callbacks for specified priority
if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) return FALSE;
// Loop through each filter for the specified priority, looking for our class & method
foreach( (array) $callbacks[ $priority ] as $filter_id => $filter ) {
// Filter should always be an array - array( $this, 'method' ), if not goto next
if ( ! isset( $filter[ 'function' ] ) || ! is_array( $filter[ 'function' ] ) ) continue;
// If first value in array is not an object, it can't be a class
if ( ! is_object( $filter[ 'function' ][ 0 ] ) ) continue;
// Method doesn't match the one we're looking for, goto next
if ( $filter[ 'function' ][ 1 ] !== $method_name ) continue;
// Method matched, now let's check the Class
if ( get_class( $filter[ 'function' ][ 0 ] ) === $class_name ) {
// Now let's remove it from the array
unset( $callbacks[ $priority ][ $filter_id ] );
// and if it was the only filter in that priority, unset that priority
if ( empty( $callbacks[ $priority ] ) ) unset( $callbacks[ $priority ] );
// and if the only filter for that tag, set the tag to an empty array
if ( empty( $callbacks ) ) $callbacks = array();
// If using WordPress older than 4.7
if ( ! is_object( $wp_filter[ $tag ] ) ) {
// Remove this filter from merged_filters, which specifies if filters have been sorted
unset( $GLOBALS[ 'merged_filters' ][ $tag ] );
}
return TRUE;
}
}
return FALSE;
}
} // End function exists
if ( ! function_exists ( 'remove_class_action' ) ) {
/**
* Remove Class Action Without Access to Class Object
*
* In order to use the core WordPress remove_action() on an action added with the callback
* to a class, you either have to have access to that class object, or it has to be a call
* to a static method. This method allows you to remove actions with a callback to a class
* you don't have access to.
*
* Works with WordPress 1.2 - 4.7+
*
* @param string $tag Action to remove
* @param string $class_name Class name for the action's callback
* @param string $method_name Method name for the action's callback
* @param int $priority Priority of the action (default 10)
*
* @return bool Whether the function is removed.
*/
function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
remove_class_filter( $tag, $class_name, $method_name, $priority );
}
} // End function exists
remove_class_action( 'wpforms_process_entry_save','WPForms_Pro', 'entry_save');
C:\xampp\htdocs\huong\wp-content\themes\woovina\inc\woocommerce\woocommerce-config.php
add_filter('wp_nav_menu_items', array($this, 'menu_cart_icon') , 10, 2);
C:\xampp\htdocs\huong\wp-content\themes\woovina-child\functions.php
function remove_class_action($tag, $class = '', $method, $priority = null) : bool {
global $wp_filter;
if (isset($wp_filter[$tag])) {
$len = strlen($method);
foreach($wp_filter[$tag] as $_priority => $actions) {
if ($actions) {
foreach($actions as $function_key => $data) {
if ($data) {
if (substr($function_key, -$len) == $method) {
if ($class !== '') {
$_class = '';
if (is_string($data['function'][0])) {
$_class = $data['function'][0];
}
elseif (is_object($data['function'][0])) {
$_class = get_class($data['function'][0]);
}
else {
return false;
}
if ($_class !== '' && $_class == $class) {
if (is_numeric($priority)) {
if ($_priority == $priority) {
//if (isset( $wp_filter->callbacks[$_priority][$function_key])) {}
return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
}
}
else {
return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
}
}
}
else {
if (is_numeric($priority)) {
if ($_priority == $priority) {
return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
}
}
else {
return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
}
}
}
}
}
}
}
}
return false;
}
add_action( 'init', 'remove_filters' );
function remove_filters(){
remove_class_action('wp_nav_menu_items', 'WooVina_WooCommerce_Config', 'menu_cart_icon', 10);
}
PreviousCustomizer radio image control (ok)NextMột siêu phẩm Post Type & Taxonomy same slug issue (ok)
Last updated