Advertisement
  1. Code
  2. WordPress
Code

Fifty Actions of WordPress – An Introduction

by
Difficulty:BeginnerLength:MediumLanguages:
This post is part of a series called Fifty Actions of WordPress.
Fifty Actions of WordPress – 50 Examples (1 to 10)

The reason of WordPress being the most popular content management system in the world is, of course, its flexibility. The beautifully simple structure of "hooks" gives WordPress this ability. Without filter hooks and action hooks, WordPress wouldn't be extendible as much and we wouldn't be able to have such a large spectrum of WordPress plugins or themes.

In this series, we're going to go through actions, one of the two kinds of WordPress hooks. Over the course of the series, we'll cover almost everything there is to know about actions:

  • In the first part, which also happens to be this article, we're going to learn what WordPress actions are and how to use actions with seven core functions.
  • In the next five parts, we're going to see a whopping set of examples about WordPress actions. In each part, we're going to go through 10 actions by learning what they're good for and doing one example for each action.
  • In the series finale, we'll be recapping what we learned and the 50 action examples we did.

It's going to be long, incredible ride. Buckle up!

What is a WordPress Action?

In the Codex, actions are defined as follows:

Actions are triggered by specific events that take place in WordPress, such as publishing a post, changing themes, or displaying an administration screen. Action is a custom PHP function defined in your plugin (or theme) and hooked, i.e. set to respond, to some of these events.

So, in essence, actions are functions triggered by a WordPress event and run before or after the event. Actions are one of the two kinds of "hooks" in WordPress – the other one is called "filters" which we covered a while ago – check out the "Fifty Filters of WordPress" if you want to learn more about WordPress filters.

Using Actions in WordPress

Defining what actions are is the easiest thing we'll do in this series. Luckily, learning how to create and use actions is almost as easy. Let's see how we hook functions to actions, create new actions, remove existing ones and other action-related functions in the WordPress core.

Hooking a Function to an Action

Probably the easiest function among these is this one: It's used to hook our function to the action hook we're going to use:

Let's see what those parameters mean:

  • $hook_name is the name of the action hook we're going to use
  • $function_name is the name of our function
  • $priority is an integer we specify to set how early the function's going to be run in the action hook
  • $arguments is the number of arguments that our function uses and the action hook allows

Let's see a quick example:

Almost too easy, right?

Removing Functions from an Action Hook

We have two functions to remove functions from action hooks: remove_action() and remove_all_actions(). Let's see how we use them:

The remove_action() function has three parameters: the name of the hook, the name of the function that we're going to remove from the hook, and the priority that's been set while using add_action(). The remove_all_actions() function doesn't have the $function_name parameter, since it removes all functions hooked to the action.

Creating an Action Hook

If you develop WordPress plugins or themes, its good practice to create some hooks in your code so that other developers can extend your plugin or theme. To do so, we have two functions again: do_action() and do_action_ref_array(). Let's see how the first one works:

As you can see, the function virtually has an infinite number of parameters since you can define as many arguments as you need. If you need to keep your arguments in an array, though, you need to use the second function:

Retrieving the Number of Times an Action Is Fired

If you ever need to know how many times an action is called, you can use this handy function to count action calls:

As you can see, the only parameter the function has is the name of the action hook.

Checking if Anything's Hooked to an Action

Imagine you're developing an extension for a popular WordPress plugin and you need to check if the user has installed the plugin or not. (After all, your plugin will not work if the other one is not installed and activated.) 

To check it, you can use the has_action() function to make sure that the popular plugin's action is available:

Just like the did_action() function, the has_action() function has only one parameter: the name of the action hook.

Conclusion

Now we've covered pretty much everything about creating and using actions, we can continue to a five-articles-long journey of action examples. We'll be reviewing only fifty of them but if you're curious, you can find hundreds more in the core.

I hope you'll enjoy this series as much as I did while creating the whole thing. If you think you can help me with the tutorials by suggesting more actions and asking for more examples, don't hesitate to tell me and share your thoughts by commenting below. And if you liked what you read in this series, don't forget to share the articles!

See you in the next tutorial!

Advertisement
Advertisement
Looking for something to help kick start your next project?

Envato Market has a range of items for sale to help get you started.