
PHP Higher Order functions
Let’s talk about functions
In order (no pun intended) for me to better explain what a higher order function is I would like to share a little bit on functions. There are two kinds of functions user-defined and built-in functions. built-in functions are core PHP or platform define such as WordPress. User-defined functions are, you guessed it defined by the developer creating the awesome new feature.
Example Code:
// Built-in function from PHP
array_filter();
// User defined function
function theme_setup() {
...code here
}
What are first-class functions?
Moving past this point, there are descriptions that stand to allow people to recognize more specifically what the person is referring to when sharing about functions. Let me start with first-class functions which PHP does support and a first-class function could be used with either user-defined or a built-in function. I mention that PHP supports first-class functions because if you didn’t already know, now you know, Javascript also supports first-class functions.
Example Code:
// First-class function
$test = function() {
$getit = 'I finally get it';
return $getit;
};
Now let’s talk about anonymous functions in PHP
This first-class function is a function that is placed inside of a variable and dynamically invoked. Getting into another description the function that is being loaded into the variable in the example above is known as an anonymous function. It is anonymous due to the function not having a name by which to call it. I am sure that since this is called an anonymous function you may be thinking what is not considered an anonymous function.
Example Code:
// Normal function NOT an anonymous function
function theme_setup() {
...code here
}
This is a normal declared function that has a name.
What are higher-order functions in PHP?
Example Code:
// Build-in function PHP core
array_filter();
This is a built-in function. What that means is that it is a part of the PHP library for us to use.
Example Code:
// Build-in function WordPress core
apply_filter();
This function is built-in on the WordPress platform. These are both higher-order functions, This is due to the fact that they both can take a function as a parameter.
add_action() add_filter() apply_filters() are some of the common higher-order functions that we use in WordPress on a day-to-day basis. Let’s walk through this example that we get from the WordPress documentation.
Example Code:
// Our filter callback function
function example_callback( $string, $arg1, $arg2 ) {
// (maybe) modify $string
return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 3 );
/*
* Apply the filters by calling the 'example_callback' function we
* "hooked" to 'example_filter' using the add_filter() function above.
* - 'example_filter' is the filter hook $tag
* - 'filter me' is the value being filtered
* - $arg1 and $arg2 are the additional arguments passed to the callback.
$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
From the example above let’s take the time to get into some specific pointers that I would like to point out for you.
How to use higher-order functions with WordPress
Example Code:
// Our filter callback function
function example_callback( $string, $arg1, $arg2 ) {
// (maybe) modify $string
return $string;
}
When writing a function you can create your own name, in the example above example_callback can be my_new_function or whatever you want it to be. Also, after the function inside the parentheses, you can set up how many parameters that are required to run your custom function.
How to register a filter with add_filter()
Example Code:
// WordPress core higher-order function
add_filter( 'example_filter', 'my_new_function', 10, 3 );
In this example, the first parameter example_filter can be changed to my_custom_filter or anything that you want it to be. The add_filter function registers your custom filter to your instance of your WordPress installation. The next parameter is your custom callback function my_new_function. After that parameter, the next one in the third position is the priority. Finally, the last parameter registers how many parameters your function my_new_function requires to run. In hopes of being very clear make note that the add_filter function does not actually run your custom function it simply registers your custom function with your WordPress installation. Now let’s look at how to actually use your custom filter.
How to use a filter with apply_filters()
Example Code:
// WordPress core higher-order function
apply_filters( 'my_custom_filter', 'your message', array( 'name'=>'Bob' ), array( 'age'=>30 ) );
Now in order to use your custom filter, we are using the built-in WordPress function apply_filters() to invoke the filter. When we use this function we need to send in the three required parameters that are registered with the custom filter. The first parameter of the apply_filters() function is your custom filter name, in this case, it is my_custom_filter. Depending on the number of required parameters you register with your custom filter you will have to load all of them after the name of your filter, in this case, we have three. In the example above we are loading your message as a string, an array with the key as name and Bob as the value, next we loaded another array with age as the key and 30 as the value.
Read more about this in the WordPress codex https://developer.wordpress.org/reference/functions/apply_filters/