apply_filters_ref_array() WordPress Function

The apply_filters_ref_array() function in WordPress allows you to "hook" into the data of another function and modify it. This is useful if you want to add or remove data from another function without having to edit the original function. To use apply_filters_ref_array(), you need to first identify the function you want to hook into. Then, you create a callback function that will modify the data. Finally, you call apply_filters_ref_array() and pass it the name of the function and your callback function. Here's an example of how to use apply_filters_ref_array(): function my_callback_function( $data ) { // modify $data return $data; } add_filter( 'the_content', 'my_callback_function' ); In this example, the my_callback_function() function will modify the data of the the_content() function.

apply_filters_ref_array( string $hook_name, array $args ) #

Calls the callback functions that have been added to a filter hook, specifying arguments in an array.


Description

Top ↑

See also

  • apply_filters(): This function is identical, but the arguments passed to the functions hooked to $hook_name are supplied using an array.

Top ↑

Parameters

$hook_name

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

$args

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


Top ↑

Return

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


Top ↑

More Information

  • 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!
  • 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:
    add_filter('my_filter', 'my_callback');
    function my_callback( $args ) {
        //access values with $args[0], $args[1] etc.
    }
    

    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:
    add_action('my_filter', 'my_callback', 10, 4 );
    function my_callback( $arg1, $arg2, $arg3, $arg4 ) {
        //access values with $args1, $args2 etc.
    }
    

    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 ));
    
    add_action('my_filter', 'my_callback');
    function my_callback( &$args ) {
        //access values with $args[0], $args[1] etc.
    }
    

    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.


Top ↑

Source

File: wp-includes/plugin.php

function apply_filters_ref_array( $hook_name, $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 $args[0];
	}

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

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

	array_pop( $wp_current_filter );

	return $filtered;
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.