Filters are functions that WordPress uses to pass data through. Passing data through filters allows developers to modify the default behavior of a specific function. Functions used to filter data are called hooks. Filters and Actions together allow developers great flexibility to modify default WordPress filters and actions and even create their own custom filters and actions so that other developers can extend their plugins or themes.
Filters are different than Actions. WordPress actions are executed at events like when a theme or plugin is activated, or when a post is published. Filters are used to filter output when it is sent to either database or to user browser.
Example of using a WordPress filter
function wpb_custom_excerpt( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= wpb_continue_reading_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'wpb_custom_excerpt' );
The sample code above adds a function wpb_custom_excerpt
to the filter get_the_excerpt
.
WordPress plugin API has an extensive list of filter hooks available in WordPress.
This post was originally published in the wpbeginner glossary.