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

wordpress函数apply_filters_ref_array()用法示例

apply_filters_ref_array( string $tag,  array $args )

Execute functions hooked on a specific filter hook, specifying arguments in an array.


描述


参数

$tag

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

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

$args

(array)
(Required)
The arguments supplied to the functions hooked to $tag.


返回值

(mixed) The filtered value after all hooked functions are applied to it.


源代码

File: wp-includes/plugin.php

function apply_filters_ref_array($tag, $args) {
	global $wp_filter, $wp_current_filter;

	// Do 'all' actions first
	if ( isset($wp_filter['all']) ) {
		$wp_current_filter[] = $tag;
		$all_args = func_get_args();
		_wp_call_all_hook($all_args);
	}

	if ( !isset($wp_filter[$tag]) ) {
		if ( isset($wp_filter['all']) )
			array_pop($wp_current_filter);
		return $args[0];
	}

	if ( !isset($wp_filter['all']) )
		$wp_current_filter[] = $tag;

	$filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args );

	array_pop( $wp_current_filter );

	return $filtered;
}

更新日志

Version 描述
3.0.0 Introduced.

相关函数

Uses

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

Used By

  • wp-includes/class-wp-network-query.php:
    WP_Network_Query::get_networks()
  • wp-includes/class-wp-network-query.php:
    WP_Network_Query::get_network_ids()
  • wp-includes/class-wp-site-query.php:
    WP_Site_Query::get_sites()
  • wp-includes/class-wp-site-query.php:
    WP_Site_Query::get_site_ids()
  • wp-includes/plugin.php:
    apply_filters_deprecated()
  • wp-includes/class-wp-comment-query.php:
    WP_Comment_Query::get_comment_ids()
  • wp-includes/class-wp-comment-query.php:
    WP_Comment_Query::get_comments()
  • wp-includes/class-wp-query.php:
    WP_Query::set_found_posts()
  • wp-includes/class-wp-query.php:
    WP_Query::get_posts()
  • wp-includes/class-wp-meta-query.php:
    WP_Meta_Query::get_sql()
  • 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

    Example

    Call added filters and pass an array of arguments:

    
    $args = array( 'arg_1', true, 'foo', 'arg_4' );
    
    apply_filters_ref_array( 'my_filter', $args );
    

    This is the same as:

    
    apply_filters( 'my_filter', 'arg_1', true, 'foo', 'arg_4' );
    
  2. Note

    This function can be useful when your arguments are already in an array, and/or when there are many arguments to pass. Just make sure that your arguments are in the proper order!

    As of PHP 5.4, the array is no longer passed by reference

    Before PHP 5.4, your callback is passed a reference pointer to the array. Your callback can use this pointer to access all the array elements. Adding a filter and declaring a call back that hooks the above example filter could look like this:

    
    function wpdocs_my_callback( $args ) {
        // Access values with $args[0], $args[1] etc.
    }
    add_filter( 'my_filter', 'wpdocs_my_callback' );
    

    Because the array was passed by reference, any changes to the array elements are applied to the original array outside of the function’s scope.

    Regardless of PHP version, you can specify the number of array elements when adding the filter, and receive each element in a separate parameter in the callback function declaration like so:

    
    function wpdocs_my_callback( $arg1, $arg2, $arg3, $arg4 ) {
        // Access values with $args1, $args2 etc.
    }
    add_action( 'my_filter', 'wpdocs_my_callback', 10, 4 );
    

    This method copies the array elements into the parameter variables. Any changes to the parameter variables do not affect the original array.

    As of PHP 5.4, the array is no longer passed by reference despite the function’s name. You cannot even use the reference sign ‘&’ because call time pass by reference now throws an error. What you can do is pass the reference pointer as an array element. Doing so does require all callbacks added to the filter to expect a reference pointer. This is not something you will see in WordPress actions. This technique is provided for informational purposes only.

    
    apply_filters_ref_array( 'my_filter', array( &$args ) );
    
    function wpdocs_my_callback( &$args ) {
        //access values with $args[0], $args[1] etc.
    }
    add_action('my_filter', 'wpdocs_my_callback');
    

    Because the original array was passed by reference, any changes to the array elements are applied to the original array outside of the function’s scope.

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

热门推荐

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

反馈