WordPress actions

WordPress actions

Online by JSC0d3
June 20, 2017 | | | 417 recognitions

In WordPress code there are numerous predefined actions or hooks that allow developers to add their own code at these points. These are a part of what makes WordPress so extensible and most plugins depend on actions for their operation.

What is: Action

An action is a function in WordPress code that is run at certain points throughout the WordPress core.

The functions that theme and plugin developers most commonly use are these:

These functions are well-known, well-documented, and used abundantly in a majority of themes and plugins. The Codex Plugin API page provides some basic examples of WordPress hooks in action. The Plugin API’s source code has made itself at home in the /wp-includes/plugin.php file. Feel free to open it in your favorite text editor or view it online to follow along.

The Plugin API is made available in the earliest stages of the WordPress boot-up process, and the earliest action one can hook into is muplugins_loaded, which is fired off after all “must use” and network-wide plugins are included — rather useless if your plugin is neither of those. The plugins_loaded action is fired off immediately after all valid plugin files are included in the scope. Finally, after_setup_theme is fired off once the active template functions.php has been included.

Actions are usually fired off via the do_action() function, which has the following prototype:

function do_action($tag, $arg = '', ...)

First of all, the $wp_actions global keeps track of how times a particular action has been triggered. It’s a simple associative array, with the action tag or name as its keys. The did_action() function (line 423) returns the number of times that an action has been triggered by accessing this $wp_actions array. Next, the all action is triggered by the _wp_call_all_hook() function. This function simply pulls on all of the registered or added hooks in the $wp_filter['all'] array using the PHP call_user_func_array() function (we will look at a great application of the all action a bit later). This is followed by a simple “check and return if action does not exist.”

Next, you’ll notice that the action tag is pushed into the global $wp_current_filter array. The current_filter() function (line 306) returns the last value stored in this global array. Action and filter callbacks can pull on more actions and filters, invoking other callbacks, resulting in long chains. One can trace the chain of hooks by looking into the global $wp_current_filter array during the execution of a callback. However, these chains do not usually get any longer than a couple of links, unless you do this:

if(!function_exists('js_function')){
	function js_function() { 
		do_action( 'js_action_2' );
	}
}
add_action( 'js_action', 'js_function' );
if(!function_exists('js_function_2')){
	function js_function_2() { 
		do_action( 'js_action_3' ); 
	}
}
add_action( 'js_action_2', 'js_function_2' );
if(!function_exists('js_function_3')){
	function js_function_3() { 
		do_action( 'js_action_4' );
	}
}
add_action( 'js_action_3', 'js_function_3' );
if(!function_exists('js_function_4')){
	function js_function_4() { 
		var_dump( $GLOBALS[ 'wp_current_filter' ] ); 
	}
}
add_action( 'js_action_4', 'js_function_4' );
do_action( 'js_action' );

/* array(4) {
[0]=> string(9) "js_action"
[1]=> string(11) "js_action_2"
[2]=> string(11) "js_action_3"
[3]=> string(11) "js_action_4"
} */

Action Hooks

Action hooks allow you to execute your custom functions which are referred to as actions. Action hooks allow you to add additional code to the WordPress core or theme so that you can achieve some new functionality or customization.

The actions or functions which you would write for your plugin or theme can be run wherever there is a hook available – which may be during the WordPress loading stage or when certain events occur.

Some examples of events where you might want to execute an action are when someone publishes a blog post or when a certain type of page is being viewed. Each of these “events” will contain an appropriate hook onto which you can attach your action (or function) to. The definition of action hooks as stated in the WordPress documentation is:

Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.

So what does an action hook look like?

In the WordPress core system hooks are usually created by writing a function which contains inside of it a command which represents the particular hook. Let’s look at an example of a very commonly used action hook called wp_head which is used by many plugins and themes to add things such as meta data between the <head> </head> tags of a WordPress page:

function wp_head() {
     do_action('wp_head');
}

(Note: The above snippet was taken directly from the core WordPress code in the file wp-includes/general-template.php).

The tell-tale characteristic which identifies the above code as an action hook is the presence of the function:

do_action('wp_head');

In other words, the do_action function is how the ‘wp_head’ action hook is created. A generic way of representing an action hook is:

do_action($tag, $args)

where:
$tag = the name of the action hook (eg, wp_head)

$args = optional parameter(s) passed to the actions (or functions written by you) which register with the particular action hook. Action hooks can have many or no parameters such as the wp_head example shown above. (Note: You should consult the WordPress code to determine if or how many parameters an action hook has)

Another way you might see an action hook created is via the following function:

do_action_ref_array( $tag, $args );

The above function differs slightly to the do_action function in that the $args variable represents an array of parameters which are mandatory.

Without going into the technical details too much, it is simply enough to know that whenever you see a do_action() or do_action_ref_array() function, then you know that you are dealing with an action hook. So now we know what action hooks look like, how do we write actions which we can use to hook into these hooks?

When you are writing your custom functions for your plugin or theme, you would typically hook your code into an action hook using a statement like the following:

add_action( 'action_hook', 'js_function_name' );

The above line tells WordPress that you would like to execute your function called your_function_name when the system comes across the action hook called action_hook.

Below is a simple example of using an action hook by creating an action:

if(!function_exists('js_action_hook_example')){
	function js_action_hook_example() {
		echo '<meta name="description" content="This meta description was created using my action hook example." />' . "\n";
	}
}
add_action( 'wp_head', 'js_action_hook_example' );

The above code is something which you might place in your theme or child-theme’s functions.php file and when it is executed it will simply echo a meta tag inside the head portion of a WordPress page.

The add_action line registers (or hooks) your action (called js_action_hook_example) to the action hook which is called wp_head.

If you recall from earlier we said that an action is a customized function which you create and in the example above you can see the function definition and the code for the function which simply adds some meta data. The example we’ve just seen uses one of a large number of action hooks available to you in WordPress and is a small drop in the ocean with what you can do with action hooks. If you are curious and want to discover more of the action action hooks available you can do a global search through the WordPress core code for the following two strings using a code editor:

  1. do_action
  2. do_action_ref_array

The search results yielding the above terms will contain the action hooks built into WordPress where the first parameter will be the action hook name. Also don’t forget that the WordPress documentation and codex is also a great source of information about hooks too.

JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | April 1, 2019

Images are vital components of every website Before you start questioning the importance of images, just try to imagine your favorite blog or website...

Posted by | March 5, 2019

I’ve been loosely following the noise and #wpdrama surrounding Gutenberg for as long as it has been around and honestly for the most part I’ve...

Posted by | February 24, 2019

To ensure that your site ranks highly in Search Engine Result Pages (SERPs), you’ll need to make it easy for search engine ‘bots’ to explore...

Previous PostBackNext Post
One impression on “WordPress actions

saints jerseys,

thank you for sharing,i like this content.

Leave here an impression