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!


Posted

in

by

Comments

One response to “add_filter and add_action”

  1. […] can use them interchangeably, but it wouldn’t follow code common sense or code “mentality” as one writer said. Filters should filter information, thus receiving information/data, applying the filter and […]

    Like

Leave a comment