
How it works...
The add_filter function is used to associate a custom plugin function to the second type of WordPress hooks, the filter hook. Filter hooks give plugins the chance to augment, modify, delete, or completely replace information while WordPress is executed. To enable this, filter functions are sent data that can be modified as a function parameter. They must return the resulting set of data back to WordPress once they have finished making the changes.
Unlike action hooks, filter functions must not output any text or HTML code, since they are executed while output is being prepared and that would likely result in the output showing up in unexpected places in the site layout. Instead, they should return the filtered data.
Taking a closer look at the parameters of the add_filter function, we can see that it is very similar to the add_action function that we saw in the previous recipes:
add_filter( 'hook_name', 'your_function_name', [priority],
[accepted_args] );
The first parameter, the hook name, indicates the name of the WordPress hook that we want our custom function to be associated with. This name must be accurately spelled; otherwise, our function will not be called and no error message will be displayed.
The second parameter is the name of the plugin function that will be called to filter data. This function can have any name, with the only condition being that this name must be unique enough to avoid conflicting with functions from other plugins or from the WordPress code.
The priority parameter is optional, as indicated by the square brackets, and has a default value of 10. It indicates the execution priority of this plugin relative to other plugins that are loaded by WordPress, with a lower number indicating a higher priority.
The last parameter of the function, accepted_args, has a default value of 1 and indicates how many parameters will be sent to your custom filter function. It should only be set to higher values when you are using filters that will send multiple parameters, as shown in this recipe with the $html and $type arguments.