in Tutorials, WordPress

Getting Started with WordPress Hooks – Actions & Filters

When I first started developing WordPress stuff years ago, I had no idea what ‘hooks’, ‘actions’ or ‘filters’ were. I saw them written about, used, discussed and explained countless times but I was afraid to try understand them. They scared me.

But I grew up. Like all good developers do, I eventually got over my fears and dedicated some time to really get my head around them. So I read a lot of guides, like this one, this one, and especially this one – it all started to make sense!

So what are hooks?

They’re little bits of code scattered through WordPress and all good plugins that allow you to ‘hook’ into the code and modify it to do custom things.

A filter allows you filter the code and modify it in someway, while an action allows you to add your own bunch of code to a specific area.

giphy

What do actions look like?

Actions are pretty simple. They’ll just be using do_action to do something like:

https://gist.github.com/bryceadams/06ed30e999b072e5185e.js

You’d then use add_action to add some code into that action! This would work:

https://gist.github.com/bryceadams/2144c40fecf2b1c250fb.js

You could also removing an action and other fun stuff, so be sure to read about the related functions.

What about filters?

Filters are a little bit more confusing. They’re also really useful, so hold your breath and dive right in!

A filter would be using apply_filters like so:

https://gist.github.com/bryceadams/9c9a4b19de90dcbbcee8.js

You’d then be able to ‘filter’ it using add_filter. You may want to add a new ingredient to the list, so based on the hook added above, this would work:

https://gist.github.com/bryceadams/ae19330fd266e8b9906f.js

Again, there’s a lot more you can do with filters, so check out the related functions.

Anything else?

Sure! This is a really vast, comprehensive part of WordPress, so read all about it!

Another nice thing to know about filters specifically is how easy WordPress makes it to quickly ‘return’ a few different things.

For example, the following filter has been provided in a plugin:

https://gist.github.com/bryceadams/bc896ae59b4229aa936f.js

So you want to quickly tell it “NO.. DON’T DISPLAY THE INGREDIENTS”. You could do the same as we did before and create a function that returns false, but that’s only for losers.

Instead, use WordPress’ included __return_false function. Like so:

https://gist.github.com/bryceadams/ce3be6f7c5349b4235ca.js

So at your disposal is:

  • __return_false()
  • __return_true()
  • __return_zero()
  • __return_empty_array()
  • __return_null()
  • __return_empty_string()

Wait plugin / theme developers!

Please take the time to add actions / filters through your product. WordPress thankfully as a lot, making it really easy to extend and built on top of. You should do the same. Don’t be a monster.

giphy (1)

Write a Comment

Comment

Webmentions

  • Easily Change WooCommerce Shipping Prices - Bryce Adams

    […] Getting Started with WordPress Hooks – Actions & Filters […]

  • Adding a WooCommerce Setting - Bryce Adams

    […] Getting Started with WordPress Hooks – Actions & Filters December 15, 2014 […]