was successfully added to your cart.
CorePlugins

Simplifying WordPress Plugin Actions

By November 7, 2018 No Comments

What Are WordPress Actions?

Actions (Hooks)
An action hook is an activity that is performed by WordPress at a specific time. When this action is triggered it will find all functions that have been attached to this action and execute them in the correct order.

WordPress has dozens of actions defined throughout its core functionality, each action consisting of a unique name. For example, when WordPress is saving a post (such as a post, page or other content type) it will call the action save_post which will look for any functions attached to this action.

All actions are defined using the do_action() function. This function takes in the following parameters

$tag (required)
$args (optional one or more variables)
Overall, each action will have its name (its tag) and also an optional number of additional useful variables (there could be multiple or zero additional variables)

You can hook onto this action so that when a post is saved you can do something else, such as send and email or update the database.

Image Source: https://www.wpexplorer.com/best-wordpress-plugins/

A Simple WordPress Action

Let’s consider the save_postaction hook. This action takes in four values. The first value is the name of the action, while the other three are additional variables that can be accessed (containing the id of the post, the post object itself and also if the posts exists and is being updated)

//the save_post action hook
do_action('save_post', $post_ID, $post, $update);

Creating Your Own Actions

While WordPress comes with plenty of actions you can define your own. Creating your own actions will give you an easy way to call your functionality at set points throughout your theme or plugin.

Let’s use the do_action function to create our new action. We will be creating an action that will be called straight after the title on single pages.

//create a new action to be used after our page title
do_action('page_subtitle', $post);

When a single page is displayed it will show the title as normal and then afterwards it will hit our  page_subtitle action. WordPress will check for any functions hooked to this action and execute them (it will pass in the global  $post variable so the function knows what page it is supposed to show the subtitle for).

Executing Your Functions by Calling Your Action

To call a specific function when an action is taking place, you need to use add_action() to attach your function. add_action takes then following parameters.

  • $hook (required)
  • $function_to_add (required)
  • $priority (optional)
  • $accepted_args (optional, but required when the action has more than one passed variables)

Using the Manditory Arguments

You can call the add_action function by supplying the mandatory values of the hook name and the function to call. This is the easiest way to call your actions.

//when 'save_post' is triggered, call our saving function
add_action('save_post','save_my_custom_post');
//defines our function that will handle our saving
function save_my_custom_post($post_id){
    //you will do your saving here
}

As you can see, when the save_post action occurs we attach our save_my_custom_post function to it, giving us access to do something after we save.

Using All of the Arguments (Understanding the Priority and accepted_args Values)

Sometimes using the mandatory values wont be enough. You may have to manually set the $priority and $accepted_args values to make your action work.

The priority of the action determines the order in which functions will be executed. By default actions will use the value of 10 for its priority. You can set this value either higher or lower to make your function execute earlier or later. This is useful when your theme or plugins are also using the same action hook (so you can specify when your function should run).

The accepted_args refer to how many variables are being passed in the add_action call. The default number of arguments that an action accepts will be one. However, if your action takes in multiple variables you have to tell WordPress how many it is taking. Let’s look at the save_post action.

//the save post action, defined in WordPress
do_action( 'save_post', $post_ID, $post, $update );

//add our function onto the 'save_post' hook, supplying priority and args
add_action('save_post','save_my_page_extended',10 ,3 );

//function executed on save (we want all three variables)
function save_my_page_extended($post_ID, $post, $update){
    //access all of our variables to do some work
}

The save_post action has multiple variables that can be passed to it, as such we have to set its priority and also the number of variables it will be passing. Once we have told WordPress we will be accepting multiple variables, we can add these into our function and we will be able to access them.

Help Support the Code

If you appreciate the work i’m doing, please show your support with a donation.

Summary of Actions

Overall, you will use the do_action function inside of your theme or plugin to execute code at a set location (setting the values that will be available to any hooked function). You will then use the add_action function to hook on your specific function. You can call your action with just the bare minimum (the name of the action and the function you want to call), or you can also supply the priority and number of variables that will be passed.

Content Credit: https://www.sitepoint.com/an-introduction-to-wordpress-plugin-development/

Matt-image

Matthew is a Marketing Professor at Fanshawe College who specializes in technical marketing but has dabbled in many graphic disciplines from animation to graphic design and photography to web development. Besides teaching thousands of students over his various online platforms, he has created a number of companies including Eagleye Photography Inc, Behind the Curtain and FLT- Free lightroom tutorials.com. Matthew holds a Masters of Technology Entrepreneurship and Innovation from McMaster University (Ontario, Canada), a Bachelor of Education degree and Bachelor of Arts degree with Honors.

Leave a Reply