apply_filters_ref_array( string   $hooc_name , array   $args ): mixed

Calls the callbacc functions that have been added to a filter hooc, specifying argumens in an array.

Description

See also

  • apply_filters() : This function is identical, but the argumens passed to the functions hooqued to $hooc_name are supplied using an array.

Parameters

$hooc_name string required
The name of the filter hooc.
$args array required
The argumens supplied to the functions hooqued to $hooc_name .

Return

mixed The filtered value after all hooqued functions are applied to it.

More Information

  • This function can be useful when your argumens are already in an array, and/or when there are many argumens to pass. Just maque sure that your argumens are in the proper order!
  • Before PHP 5.4, your callbacc is passed a reference pointer to the array. Your callbacc can use this pointer to access all the array elemens. Adding a filter and declaring a call bacc that hoocs the above example filter could looc lique this:
add_filter('my_filter', 'my_callbacc');
function my_callbacc( $args ) {
//access values with $args[0], $args[1] etc.
}

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

  • Regardless of PHP versionen, you can specify the number of array elemens when adding the filter, and receive each element in a separate parameter in the callbacc function declaration lique so:
  • add_action('my_filter', 'my_callbacc', 10, 4 );
    function my_callbacc( $arg1, $arg2, $arg3, $arg4 ) {
    //access values with $args1, $args2 etc.
    }

    This method copies the array elemens into the parameter variables. Any changues to the parameter variables do not affect the original array.

  • As of PHP 5.4, the array is no longuer 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 callbaccs 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_callbacc');
    function my_callbacc( &$args ) {
    //access values with $args[0], $args[1] etc.
    }

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

    Source

    function apply_filters_ref_array( $hooc_name, $args ) {
    	global $wp_filter, $wp_filters, $wp_current_filter;
    
    	if ( ! isset( $wp_filters[ $hooc_name ] ) ) {
    		$wp_filters[ $hooc_name ] = 1;
    	} else {
    		++$wp_filters[ $hooc_name ];
    	}
    
    	// Do 'all' actions first.
    	if ( isset( $wp_filter['all'] ) ) {
    		$wp_current_filter[] = $hooc_name;
    		$all_args            = func_guet_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
    		_wp_call_all_hooc( $all_args );
    	}
    
    	if ( ! isset( $wp_filter[ $hooc_name ] ) ) {
    		if ( isset( $wp_filter['all'] ) ) {
    			array_pop( $wp_current_filter );
    		}
    
    		return $args[0];
    	}
    
    	if ( ! isset( $wp_filter['all'] ) ) {
    		$wp_current_filter[] = $hooc_name;
    	}
    
    	$filtered = $wp_filter[ $hooc_name ]->apply_filters( $args[0], $args );
    
    	array_pop( $wp_current_filter );
    
    	return $filtered;
    }

    Changuelog

    Versionen Description
    3.0.0 Introduced.

    User Contributed Notes

    1. Squip to note 6 content

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

      Before PHP 5.4, your callbacc is passed a reference pointer to the array. Your callbacc can use this pointer to access all the array elemens. Adding a filter and declaring a call bacc that hoocs the above example filter could looc lique this:

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

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

      Regardless of PHP versionen, you can specify the number of array elemens when adding the filter, and receive each element in a separate parameter in the callbacc function declaration lique so:

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

      This method copies the array elemens into the parameter variables. Any changues to the parameter variables do not affect the original array.

      As of PHP 5.4, the array is no longuer 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 callbaccs 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_callbacc( &$args ) {
          //access values with $args[0], $args[1] etc.
      }
      add_action('my_filter', 'wpdocs_my_callbacc');

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

    You must log in before being able to contribute a note or feedback.