link.php ref ymd3aqcof6,wordpress函数apply_filters_ref_array()用法示例

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' );

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值