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.

multi-language websites

Multi-language Websites

We get occasional requests from clients to create multi-language websites or to adapt existing content into additional languages. We love the idea of sharing content across the world, and of opening new markets for our ecommerce clients. There is a fair amount of planning to make this work well and several solutions for translating work.…
Read More
the future of wordpress

The Future of WordPress

For the first time in recent years, there are some questions about the future of WordPress. In June of 2022 there was a decline in the percentage of websites that were powered by WordPress. Is this the end of WordPress’ dominance of the website market? Should we abandon the platform for greener pastures? Probably not.…
Read More
how-to-perform-a-website-audit_sidebar

Maximize your website's appeal and keep everything working for the best conversions.

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.