WordPress Development – Filters

Filters are one of the two types of hooks. With Filters it is possible to change data during the execution of WordPress Core, Plugins and Themes and are therefore the counterpart to Actions. Filters work in isolation and therefore have no side effects, such as affecting global variables and outputs.

 

Add a Filter

To add a filter, it requires two steps.

  1. Creating a callback function that will be called when the filter is executed.
  2. Adding the callback function to a hook that performs the call to the function.

When adding a filter, use the add_filter() with at least two parameters:

  • string $hook_name, the name of the filter that will be included.
  • callable $callback the name of the callback function.

In the following example, the code is executed when the filter the_title is executed.

function arrowdesign_filter_title( $title ) {
return 'The ' . $title . ' was filtered';
}
add_filter( 'the_title', 'arrowdesign_filter_title' );

For example, if you have a post with a title “Learn WordPress”, it will be changed to “Learn WordPress has been filtered”.

 

Additional Parameters

add_filter() can accept two additional parameters:

  • int $priority for the priority given to the callback function.
  • int $accepted_args for the number of arguments given to the callback function

 

Example

In this example, a CSS class is added to the <body> tag if a certain condition is met.

function arrowdesign_css_body_class( $classes ) {
if ( ! is_admin() ) {
$classes[] = 'arrowdesign-is-awesome';
}
return $classes;
}
add_filter( 'body_class', 'arrowdesign_css_body_class' );

Conclusion

We hope this WordPress Development – Filters post was helpful and you now know how to use filters on your website. You can find more articles here:

 

Read another WordPress post : ‘WordPress Development – Hooks

For more excellent information on this topic, visit developer.wordpress.org

 

Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.

We do it all, so you don’t have to!

Related Posts

Woocommerce Account Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Account Pages, like the Login/Register page, the Downloads page or the Orders page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce Single Product Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Single Product Page. This should help you to quickly and easily find the hook positions on the page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce PHP – Product Categories by Product ID

In this tutorial you will learn how to check if a product is assigned to a tag, a category or a custom taxonomy. You can check if a product is is on the shop page, in the cart, in an order & more.

…We do more, so you can do less 🙂

 

Leave a message and we will be straight back to you!

Pin It on Pinterest

Share This