Skip to content

Instantly share code, notes, and snippets.

@greatislander
Last active May 19, 2017 16:01
Show Gist options
  • Save greatislander/1999501a8145e6d2c161a027b3d28d7d to your computer and use it in GitHub Desktop.
Save greatislander/1999501a8145e6d2c161a027b3d28d7d to your computer and use it in GitHub Desktop.
<?php
// Array with filter hook.
$my_array = apply_filters( 'filter_my_array', array(
'value1',
'value2',
'value3',
) );
/**
* Filter function
*
* @param array $array The input array.
* @return array The filtered array.
*/
function filter_array( $array ) {
$array[] = 'value4'; // Add another item to the array.
return $array; // Return the array with the new item.
}
// Attaching our function to the filter hook.
add_filter( 'filter_my_array', 'filter_array' );
// OR use an anonymous function (this does the same thing)
add_filter( 'filter_my_array', function ( $array ) {
$array[] = 'value4'; // Add another item to the array.
return $array; // Return the array with the new item.
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment