Tuesday , 18 June 2019
Home / Programming / WordPress / Plugins / Filter vs Action Hooks – WordPress Plugin Tutorial
Filter vs Action Hooks
Filter vs Action Hooks

Filter vs Action Hooks – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

So far, we’ve learned how to use an action hook and a filter hook.  Now, some of you might be thinking, what’s the difference? Filter vs action hooks is a common debate between developers and themselves.

Action Hooks

Action hooks can also take in data just like a filter hook. Action hooks are aimed at running behind the scenes. For example, what if someone comments on a post you made? You can create a plugin that’ll e-mail you when someone comments on your post. You can also use action hooks to update a database or update some settings.

Can you use action hooks to insert HTML? Of course! If you don’t feel safe or don’t feel the need to filter content, then you can use an action hook to help you add any output on your WordPress site.

Filter Hooks

Filter hooks are meant to take in an input, modify/change it and then return it. You MUST return a value or the code will break. You don’t have to return the same data type. For example, if you’re passed a string, then you’re more than welcome to return a boolean value like false. You can also echo out content like like actions, but it’s not recommend as you may not know where it’ll appear.

Filter vs Action Hooks

You can stick to one and ignore the other, but that isn’t exactly wise. They both have different uses conceptually. If you’re going to filter something, then use a filter hook. If you only want to take action, then use action hooks. It’s truly as simple as that. Neither executes faster than the other and limiting yourself to one will limit to what you can do and make your job much more complicated.

If you need more information about action hooks, go here. If you need more information about filter hooks, go here.

In this series, we’ll be using both.

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.