add_filter and add_action

What’s the difference between add_filter and add_action?

The difference is semantic. You can use add_filter() for an action, and you can use add_action() for a filter. The difference is there because actions and filters have a different code “mentality.” Filters are just that, filters through which information is passed, and then used. For example… the contents of this entry are passed through several filters. They turn my line breaks into HTML paragraphs, and correct HTML mistakes. Most importantly, when all filters have been run, something is done with that information. With actions, information may be passed between functions that use the action hook, but nothing is done with the information after all the filters have run. An example would be when I posted this entry publish_post was fired, and passed along the ID of this post. Plugins could use that ID to do things related to this entry, and pass on the ID for other plugins, but after all the plugins have run, the information passed along (the ID) is thrown away. Actions also have the possibility of not passing on any information (“No Parameters” hooks). These are hooks that are merely placeholders… like a template header or an admin footer. These are the only hooks whose functions should not return any information. Important note: actions are not all “No Parameters” hooks! It is still important to return any information you may get when using an action hook… even though WordPress won’t be using it, other plugins might!


One thought on “add_filter and add_action

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s