What is a WordPress filter? In WordPress development, Filters allow developers to modify the default behaviors of specific functions in the WordPress core. Functions used to filter data are called hooks. Both Filters and Actions are hooks. Hooks allow developers to hook into the WordPress core to make custom themes and plugins.
However, Filters and Actions are not the same. Filters are the counterpart to Actions, which lets developers execute code at specific points throughout the WordPress core. A WordPress Filter allows developers to modify the behavior or output of functions throughout the WordPress core.
How to Add Filters in WordPress?
To give you an example of how WordPress Filters are used to add code to your WordPress site, let us pretend you need to change the number of characters the excerpt shows because your theme doesn’t have that option.
To add your custom code, you’ll want to place it into a function called a Callback and add it to your WordPress theme’s
function.php file. Then you’ll need to add your Callback function to a Hook which will call the function.
Custom Excerpt Length Example:
The sample code above creates a function
custom_excerpt_length which is hooked into
add_filter() function takes at least two parameters to satisfy it.