WordCamp San Diego 2012: Playing Hooky with Actions and Filters

Actions and Filters are collectively known as hooks. They provide developers with tools to modify and integrate into existing code. As a plugin creator you want to include hooks into your work so that your users can customize the plugin without needing to make changes to your core files. Likewise, as a user you want to leverage hooks to create those customizations. Therefore, the practice keeps you from having to fork a plugin and potentially cost yourself a lot of time and effort maintaining an alternate version of the plugin.

The two types of hooks have a lot of similarities. They both consist of a line of php with some parameters. The important takeaways for starting out are, actions do stuff, while filters change stuff.

For example, if you needed a widget to place in the sidebar of your site that displays the word “awesome.” You install a widget that provides the feature and you realize you need a puppy picture to go with it. That’s where you want to see some hooks involved. By having actions and filters you make the code flexible for others to modify.

Actions

There are several basic actions that can be written into your code to facilitate actions that can be taken in your code. As a result, they are like the building blocks of WordPress.

  • do_action – provides a place to perform and action.
  • add_action – provides a way to add a function into the code. This action has two required  parameters $tag and $function_to_add.
  • remove_action – provides a way to remove an action

These actions have some parameters that can be set that provide some ways to modify how these actions run.

  • $tag – identifies the action you want to hook onto.
  • $arg – the list of arguments this hook accepts.
  • $function_to_add – the name of the function you want to add.
  • $priority – sets the order in which actions are completed. This parameter has a default value of 10.
  • $accepted_args – how many arguments your function takes. This parameter has a default value of 1.

Filters

There are also several basic filters that provide a variety of ways to alter data.

  • apply_filters – has two required parameters: $tag and $value
  • add_filter – has four parameters: $tag, $function_to_add, $priority, and $accepted_args
  • remove_filter – has three parameters: $tag, $function_to_remove, and $priority

Similar to actions, filters have parameters that are set to describe and modify the filter. For example, these parameters are similar to action parameters, but may behave differently.

  • $tag – identifies the name of the filter hook.
  • $value – the value which the filters hooked to $tag may modify.
  • $var – one or more additional variables passed to the filter.
  • $priority – sets the order in which filters are completed. This parameter has a default value of 10.
  • $function_to_add/$function_to_remove – the name of the function called.
  • $accepted_args – additional content sent to the filter, usually building blocks for the output

In conclusion, these are just some of the basics. There are hundreds of actions and filters available for use in WordPress. Additionally, your themes or plugins may introduce extra hooks for you to leverage. Look at your documentation, review the development documents, or Google it. Finally, if you have any questions you can always contact us directly.

Similar Posts