apply_filters函数在wordpress模板开发中如何使用

更新时间:2022-06-15 19:44:15 来源:青锋建站 作者:青锋建站
  apply_filters经常用于wordpress建站开发过程中调用附加到hook中的函数。以下是青锋建站给大家分享apply_filters函数的详细使用方法。

apply_filters

  ( string $hook_name, mixed $value, mixed $args )调用已添加到过滤hook中的回调函数。

apply_filters函数简介

  此函数调用附加到过滤器钩子$hook_name的所有函数。通过简单地调用此函数,可以实现创建新的筛选器hook,使用$hook_name参数指定新hook的名称。这个函数也允许传递给这个hook多个参数。

apply_filters函数使用举例

// The filter callback function.
function example_callback( $string, $arg1, $arg2 ) {
    // (maybe) modify $string.
    return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 3 );
/*
* Apply the filters by calling the 'example_callback()' function
* that's hooked onto `example_filter` above.
*
* - 'example_filter' is the filter hook.
* - 'filter me' is the value being filtered.
* - $arg1 and $arg2 are the additional arguments passed to the callback.
$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );

apply_filters函数参数

  $hook_name:(string) (Required) 过滤hook的名称。
  $value:(mixed) (Required)要过滤的值。
  $args传递给回调函数的额外参数。

apply_filters函数返回值

  (mixed) 将所有的hook函数附加给过滤器后的值。

apply_filters函数源代码

  文件wp-includes/plugin.php
function apply_filters( $hook_name, $value, ...$args ) {
    global $wp_filter, $wp_current_filter;

    // Do 'all' actions first.
    if ( isset( $wp_filter['all'] ) ) {
        $wp_current_filter[] = $hook_name;

        $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
        _wp_call_all_hook( $all_args );
    }

    if ( ! isset( $wp_filter[ $hook_name ] ) ) {
        if ( isset( $wp_filter['all'] ) ) {
            array_pop( $wp_current_filter );
        }

        return $value;
    }

    if ( ! isset( $wp_filter['all'] ) ) {
        $wp_current_filter[] = $hook_name;
    }

    // Pass the value to WP_Hook.
    array_unshift( $args, $value );

    $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );

    array_pop( $wp_current_filter );

    return $filtered;
}
  以上就是青锋建站给大家分享的apply_filters函数在wordpress建站开发中如何使用,青锋建站承接网站建设服务,包括织梦建站,phpcms建站,wordpress建站,CMS系统开发,SEO网站优化,网络营销推广,企业邮箱,400电话。

转载请注明来源网址:青锋建站-http://www.sjzphp.com/cmsxitong/wordpressxt/apply_filters_1388.html

地方分站

电话 15632335515 | 邮箱 943703539@qq.com | QQ 943703539 | 微信 qingfengjianzhan

Copyright © 2016-2026 青锋建站 版权所有