Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
25 Million+
Websites using our plugins
Years of WordPress experience
WordPress tutorials
by experts

What is: Filter

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

In WordPress development, a filter can be used to change or extend WordPress’ functionality. It does this by taking some data provided by WordPress, changing it into something else, and then passing it back.

Filters are one of the big features that make WordPress so customizable. They are pieces of PHP code that are ‘hooked’ to a WordPress event. When the event is triggered, the filter will be applied to the data output generated by the event hook.

They are used extensively by plugin and theme developers. However, if you’re not a developer, then you can paste code snippets from the web to add new features to your website. Many of these include filters.

What Is Filter in WordPress?

What is a Filter in WordPress?

A filter will modify the default behavior of a specific function on your WordPress website. It does this by manipulating the data it receives and then returning that data to WordPress before it is displayed in the browser.

For example, filters can be used to truncate text, change the formatting of content, attach links to posts, modify blocks on a page, and change options retrieved from the database. They could also change the length of an excerpt, place related posts beneath your main content, or change a price in WooCommerce.

Filters are added using the add_filter() function. WordPress has several functions that allow you to use actions, but these are the ones that are most commonly used:

  • add_filter(): this attaches a function to a hook
  • remove_filter(): this removes a function attached to a specified filter hook
  • doing_filter(): this detects any filter currently being executed
  • has_filter(): checks to see if a filter has been registered

Filters are used by plugin and theme developers to add extra functionality to WordPress. You can use also filters to customize your theme by adding code snippets from online tutorials.

Note: If you’re a beginner, then we strongly caution against editing any WordPress files. Only experienced users who feel comfortable with editing the functions.php file and have some knowledge of PHP should try this.

Beginners should either use a plugin to accomplish the task they want to perform or consult a professional to edit code the code for them.

Before editing any code on your WordPress site we recommend that you backup your website in the event of a coding error. If you don’t have a backup plugin, then be sure to read our article where we compare the best WordPress backup plugins.

We also recommend that you don’t add the code directly to the theme files, or you will lose your customizations next time you update the theme.

Instead, you should use a code snippets plugin to add custom code in WordPress, create a site-specific plugin, or create a child theme and modify it instead of the parent theme.

See our guide on how to update a WordPress theme without losing customization.

What’s the Difference Between a Hooks, Actions, and Filters?

To get a better understanding of filters in WordPress, it’s helpful to see how they relate to two other terms, hooks and actions. Understanding these three terms together will make the term ‘filter’ a little easier to grasp.

Hooks are the foundation of WordPress plugin and theme development. They are places where developers can ‘hook’ their custom code into WordPress at specific locations and change how WordPress operates without editing core files.

There are two types of hooks.

  • Filter hooks allow you to change something. They intercept data that is being processed and let you modify it, then pass it back. They are used to filter output when it is sent to either a database or a user’s browser.
  • Action hooks allow you to do something. They let you add extra functionality and are executed when events like when a theme or plugin is activated, or when a post is published. Once the action has been performed, they don’t need to pass any information back.

These action and filter hooks are the foundation of how the WordPress core, themes, and plugins work. They work together to allow developers great flexibility to modify default WordPress events, filters, and actions.

Developers can also create their own custom actions and filters so that other developers can extend their plugins or themes.

Examples of WordPress Filters

What does a filter look like? Here are a few examples.

Let’s say we want to display an image icon when a post belongs to a particular category is displayed. In this scenario, we create a function that checks if a post is in that particular category. If it is, then display the image.

Next, we hook that function into the_content event. Now whenever the event the_content takes place, our functional is automatically triggered to filter the output of the_content event.

// First we hook our own function with the_content event
add_filter( 'the_content', 'wpb_content_filter' );
// Now we define what our function would do.
// In this example it displays an image if a post is in news category.
function wpb_content_filter( $content ) {
    if ( in_category('news') )
        $content = sprintf('<img class="news-icon" src="%s/images/news_icon.png" alt="News icon" title="" />%s', get_bloginfo( 'stylesheet_directory' ), $content);
    // Returns the content.
    return $content;

Here’s another example of using a WordPress filter.

This sample code adds a function wpb_custom_excerpt to the filter get_the_excerpt.

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' );

You can learn more by studying our WordPress guides that use filters. For example, here are a few guides that show you how to achieve something practical using WordPress filters:

WordPress has many predefined filters that allow developers to add their own code at specific points throughout the WordPress core. The WordPress Plugin API provides an extensive list of filter hooks that are available.

We hope this article helped you learn more about filters in WordPress. You may also want to see our Additional Reading list below for related articles on useful WordPress tips, tricks, and ideas.

If you liked this guide, then please consider subscribing to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Additional Reading

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!