DEV Community

Cover image for Wordpress concepts: Action and Filter hooks, power and foundation of Wordpress
Amirition
Amirition

Posted on • Edited on

Wordpress concepts: Action and Filter hooks, power and foundation of Wordpress

Let me be frank, you can't be a wordpress developer without knowing Wordpress hooks. Now what are wordpress action and filters? There's a lot of good and detailed articles out there about Wordpress action hooks and filter hooks and I'm not gonna repeat what they said before.

Some cases to use hooks

Since I'm not gonna write codes at this moment, so I think the best way to clear this concept for you is showing some examples.
Wordpress Filter Hooks
Let's assume that you want to add some text below every post content. Basically you are changing a content, this is what a filter hook means; You can change a content. But how?
Wordpress developer references or old Wordpress codex are great resources, they have all the hooks you need with detailed information and some examples. You just have to search for the right hook.
Back to our example, You just need to search for something like:Wordpress post content filter
These searches will probably lead you to Wordpress official docs and there you can find some good examples to use it.

This was the filters, you can use it to change some specific data that the filter defined for. Now let's assume that you are developing a plugin and you want to let people to change some data in this plugin. This is something possible and very standard too, you can define some filters for the data you want and another one or even yourself will be able to hook into that filter and change the data. You can find more about this at Wordpress developer site.

Wordpress Action Hooks
You want to go straight to the examples or define it with some gibberish words? OK!
You want to be notified whenever a new post publishes on your site. So you want to execute a code when a certain thing happens, right? But how Wordpress knows that when to run your code?
You use action hooks at this cases. Just like the filter hooks example, you should know what action you want to use. Search Wordpress developer site with keywords like this: after post publish action
Then you got some examples and codes that is very clear and you can understand it.
You can have your own actions too. This way you can open an easy way for the people and of course future you to easily add code in certain places of your code. You can find more about this at Wordpress developer site.

So, do you think the hooks are the main power of Wordpress? I'll be glad to hear or ideas.

Previous Article: Wordpress concepts series: be a good wordpress developer
Next Article: Who knows?

Top comments (0)