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 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 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.
In this series, we’ll be using both.