Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress PHP for WordPress More Advanced PHP for WordPress Variables in WordPress

Greg Schudel
Greg Schudel
4,090 Points

Difference between actions and filters?

I'm a little fuzzy on what the difference is between actions and filters.

I know examples are made in the video. But I'm thinking about other applications.

Are actions hooks used for inserting custom boostrap or javascript for a menu on a specific page?

Are filters used for the user to select their own widget or layout?

Please provide guidance, thank you!

2 Answers

Yes you are correct. Take a look at wp_enqueue_scripts. There you add_action of enqueueing your styles or scripts into the page. This is an example of an action hook.

Filter is when you can to add functionality to WP core to something that already exists. A good example is when you want to add a body classes to a page on your site.

Mackenzie Bowes
Mackenzie Bowes
2,237 Points

From stack exchange: In reality Filter Hooks are pretty much a superset of Action Hooks. The former can do anything the latter can do and a bit more albeit the developer doesn't have the responsibility to return a value with the Action Hook that he or she does with the Filter Hook.

But that's probably not what is important. I think what is important is that by a developer choosing to use an Action Hook vs. a Filter Hook or vice versa they are telegraphing their intent and thus giving guidance to the themer or plugin developer who might be using the hook. In essence they are saying either "I'm going to call you, do whatever you need to do" OR "I've going to pass you this value to modify but be sure that you pass it back."

So ultimately I think that guidance provided by the choice of hook type is the real value behind the distinction. IMO, anyway.

  • Mike Schinkel

So, in other words, an Action Hook is a Filter Hook that has been designed to be easier to use. You can use a filter hook to do whatever you want anywhere, but Action Hooks are ways of suggesting where the people who come after you do things.