返回介绍

has_filter()

发布于 2017-09-11 00:50:20 字数 3718 浏览 1173 评论 0 收藏 0

has_filter( string $tag,  callable|bool $function_to_check = false )

Check if any filter has been registered for a hook.


description


参数

$tag

(string) (Required) The name of the filter hook.

$function_to_check

(callable|bool) (Optional) The callback to check for.

Default value: false


返回值

(false|int) If $function_to_check is omitted, returns boolean for whether the hook has anything registered. When checking a specific function, the priority of that hook is returned, or false if the function is not attached. When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false (e.g.) 0, so use the === operator for testing the return value.


源代码

File: wp-includes/plugin.php

function has_filter($tag, $function_to_check = false) {
	global $wp_filter;

	if ( ! isset( $wp_filter[ $tag ] ) ) {
		return false;
	}

	return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Used By

  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::save_changeset_post()
  • wp-includes/plugin.php: apply_filters_deprecated()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::preview()
  • wp-admin/includes/class-wp-ms-sites-list-table.php: WP_MS_Sites_List_Table::column_plugins()
  • wp-admin/includes/class-core-upgrader.php: Core_Upgrader::upgrade()
  • wp-admin/includes/class-wp-ms-sites-list-table.php: WP_MS_Sites_List_Table::get_columns()
  • wp-includes/capabilities.php: map_meta_cap()
  • wp-includes/class-wp.php: WP::build_query_string()
  • wp-includes/plugin.php: has_action()
  • wp-includes/class-wp-editor.php: _WP_Editors::editor()
  • Show 5 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

    Basic Example

    
    <?php
    if ( ! has_filter( 'the_content', 'example_alter_the_content' ) )
        add_filter( 'the_content', 'prefix_alter_the_content' );
    ?>
    
  2. add_filter() calls the same _wp_filter_build_unique_id() function and re-assigns the method/function parameter to the index array.

    It is very likely that calling add_filter() with the same parameter list is faster than first checking if the method/function is already registered with has_filter( , ) before adding it with add_filter( , );

    I am guessing the best use-case for has_filter() is to check if a filter has ANY registered methods versus checking that a specific method exists prior to re-registering it with add_filter().

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

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

发布评论

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