返回介绍

remove_action()

发布于 2017-09-11 10:07:42 字数 9148 浏览 1033 评论 0 收藏 0

remove_action( string $tag,  callable $function_to_remove,  int $priority = 10 )

Removes a function from a specified action hook.


description

This function removes a function attached to a specified action hook. This method can be used to remove default functions attached to a specific filter hook and possibly replace them with a substitute.


参数

$tag

(string) (Required) The action hook to which the function to be removed is hooked.

$function_to_remove

(callable) (Required) The name of the function which should be removed.

$priority

(int) (Optional) The priority of the function.

Default value: 10


返回值

(bool) Whether the function is removed.


源代码

File: wp-includes/plugin.php

function remove_action( $tag, $function_to_remove, $priority = 10 ) {
	return remove_filter( $tag, $function_to_remove, $priority );
}

更新日志

Versiondescription
1.2.0Introduced.

相关函数

Uses

  • wp-includes/plugin.php: remove_filter()

Used By

  • wp-includes/nav-menu.php: _wp_delete_customize_changeset_dependent_auto_drafts()
  • wp-includes/theme.php: _wp_customize_publish_changeset()
  • wp-includes/class-wp-post-type.php: WP_Post_Type::unregister_meta_boxes()
  • wp-includes/class-wp-post-type.php: WP_Post_Type::remove_hooks()
  • wp-admin/includes/class-wp-automatic-updater.php: WP_Automatic_Updater::run()
  • wp-admin/includes/class-language-pack-upgrader.php: Language_Pack_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-theme-upgrader.php: Theme_Upgrader::install()
  • wp-admin/includes/class-theme-upgrader.php: Theme_Upgrader::upgrade()
  • wp-admin/includes/class-plugin-upgrader.php: Plugin_Upgrader::install()
  • wp-admin/includes/class-plugin-upgrader.php: Plugin_Upgrader::upgrade()
  • wp-admin/includes/class-wp-ms-themes-list-table.php: WP_MS_Themes_List_Table::single_row()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::__construct()
  • wp-includes/theme.php: _remove_theme_support()
  • wp-includes/theme.php: check_theme_switched()
  • wp-includes/deprecated.php: automatic_feed_links()
  • wp-includes/rewrite.php: add_feed()
  • wp-includes/media-template.php: wp_print_media_templates()
  • Show 12 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    This function is identical to the remove_filter() function.

    
     <?php remove_action( $tag, $function_to_remove, $priority ); ?> 
    

    If an action has been added from within a class, for example by a plugin, removing it will require accessing the class variable.

    
    global $my_class;
    remove_action( 'the_content', array( $my_class, 'class_filter_function' ) );
    

    It is also worth noting that you may need to prioritise the removal of the action to a hook that occurs after the action is added. You cannot successfully remove the action before it has been added.

  2. If you need to be able to remove an action/filter for a class object you do not have access to, you can do so with this function (which includes support for WordPress 4.7+):

    
    /**
     * Make sure the function does not exist before defining it
     */
    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+ support added 9-19-2016)
    	 * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
    	 *
    	 * @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 ) ) {
    			// Create $fob object from filter tag, to use below
    			$fob       = $wp_filter[ $tag ];
    			$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 ) {
    				// WordPress 4.7+ use core remove_filter() since we found the class object
    				if ( isset( $fob ) ) {
    					// Handles removing filter, reseting callback priority keys mid-iteration, etc.
    					$fob->remove_filter( $tag, $filter['function'], $priority );
    				} else {
    					// Use legacy removal process (pre 4.7)
    					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();
    					}
    					// Remove this filter from merged_filters, which specifies if filters have been sorted
    					unset( $GLOBALS['merged_filters'][ $tag ] );
    				}
    				return TRUE;
    			}
    		}
    		return FALSE;
    	}
    }
    /**
     * Make sure the function does not exist before defining it
     */
    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+ support added 9-19-2016)
    	 *
    	 * @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 );
    	}
    }

    https://gist.github.com/tripflex/c6518efc1753cf2392559866b4bd1a53

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文