apply_filters_ref_array( string $tag, array $args )
Calls the callback functions that have been added to a filter hook, specifying arguments in an array.
Contents
Description Description
See also See also
- apply_filters(): This function is identical, but the arguments passed to the functions hooked to
$tag
are supplied using an array.
Parameters Parameters
- $tag
-
(string) (Required) The name of the filter hook.
- $args
-
(array) (Required) The arguments supplied to the functions hooked to $tag.
Return Return
(mixed) The filtered value after all hooked functions are applied to it.
Source Source
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; }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
3.0.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Example
Call added filters and pass an array of arguments:
This is the same as:
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:
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:
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.
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.