^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数has_filter()用法示例

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

Check if any filter has been registered for a hook.


描述


参数

$tag

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

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

$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 );
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

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


  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().

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈