The apply_filters() is a filter hook introduced by Wordpress core functionality. It will help to update your codes without changing the core functionality.
For example, assume you have a small list of user that you would like to print somewhere in the frontend i.e. $users = array('Jane', 'Cassie');
. A simple solution to print these users is:
$users = array('Jane', 'Cassie');
foreach($users as $user) {
echo $user; // output Jane and Cassie
}
But let's say you get more users from some third-party services then you have to implement the third-party service on the same UI page with messy codes. The better approach to solving this kind of issue is the use of filter hooks. Let's slightly modify our existing codes.
$users = array('Jane', 'Cassie');
$users = apply_filters('my_awesome_users', $users);
foreach($users as $user) {
echo $user; // output Jane and Cassie
}
Now in case of third-party service, let's say it provides us a user with the name 'Jon' then we have to call our defined filter with the help of add_filter() action hook.
add_action('my_awesome_users', 'service_one_for_jon');
function service_one_for_jon( $users ) {
// Services codes here..
$another_user = 'Jon';
array_push($users, $another_user);
return $users;
}
And the output on the UI section should be:
foreach($users as $user) {
echo $user; // output Jane, Cassie and Jon
}
I hope this can helps. Take a look at apply_filters documentation https://developer.wordpress.org/reference/functions/apply_filters/