WordCamp San Diego 2012: Playing Hooky with Actions and Filters

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.

Written by the Team at Pixel Jar

We hope you got something useful out of that post. If you'd like to read more we have an active blog with topics across the spectrum of website development. If you're researching information for a project we'd love to talk to you about it.

spamshiv

(PLUGIN) Spam Protection by SpamShiv

Since the dawn of the Internet, bots have lived amongst us. While you sleep, they are busy crawling their way from page to page, analyzing content in search of valuable information. One type of information they look for is an email address. What Are Bots Up To? Bots collect these email addresses for a number…
Read More
WordCamp San Diego 2017

WordCamp San Diego 2017

WordCamp San Diego 2017 was on March 25th and 26th. This year WCSD returned to a previous venue at the Thomas Jefferson School of Law in the heart of downtown San Diego. The camp was beautifully run and the return to the venue was a great move. The whole Pixel Jar team was able to…
Read More

How Can We Help You?

We want to build your next project.

Connect with Pixel Jar

Our Community

Subscribe to learn more about the goings on at Pixel Jar.
  • Note: Your email will be added to our CRM and be used to receive emails from Pixel Jar. You can unsubscribe at any time.

  • This field is for validation purposes and should be left unchanged.