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.

redesigning a website workflow

Redesigning a Website Workflow

We’ve had the pleasure of working with JWC on a regular basis and they approached us with a big project – a complete overhaul of their website workflow. As we dug into the project, we found that several of their core processes were costing them unnecessary time and were inefficient. This often occurs with business…
Read More
A Guide to Project Management Tools

A Guide to Project Management Tools

There are an endless supply of project management tools available to web developers. As we built our team and started taking on bigger projects, we found ourselves in need of some specific tools to help us manage the details. These are the tools that we’re currently using to manage our projects and our business. Instant…
Read More

Learn how to understand your website's strengths and weaknesses, and how to make changes for the best possible chance at success.

Download Now

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.