What Is an Action in WordPress?
In WordPress, an action is a PHP function that is registered for an action hook.
Actions provide a way for running a function at a specific point in the execution of WordPress Core, plugins, and themes.
Adding an Action
Adding an action to an action hook is done in two steps:
- Creating a callback function
- Assigning your callback function to the action hook
Creating a callback function
Before we can add an action to a hook, we need to create its callback function. This is the function that will execute when the action hook is fired.
The callback function is just a normal Php function, but however it should be prefixed uniquely and it should be somewhere callable. The number of parameters this function should accept are defined by the action hook you are hooking into.
Here is an example of a callback function:
<?php
function sc_sfm_footer_message(){
echo 'Hi, I am now a cool plugin developer';
}
?>
This function accepts no parameters and echoes a predefined message when called.
Assigning your callback function to the action hook
After creating your callback function, what is left is to add your callback function to the hook using the add_action()
function. add_action()
requires a minimum of two parameters.
add_action()
A plugin basically hooks into an action hook and provide actions that perform specific tasks when the action hook is fired. To achieve this, plugins use the add_action()
function to add an action to the do_action()
function in order to do
the action
.
This is what an action looks like:
<?php
add_action( $tag, $function, $priority, $accepted_args );
?>
$tag
— The name of the action hook where your function (action) is to be registered.$function
— The name of your function that is to be called when the action is fired.$priority
— (Optional) An integer value that represents the order in which the action is fired. It defaults to 10 if no value is given. The lower the number, the earlier the function is called. The higher the number, the later it is called.$accepted_args
— (Optional) The number of parameters the action hook will pass to your function. It defaults to 1 if no value is given.
The following is an example of a callback function sc_sdc_callback()
that is "hooked" to run when the init
hook is fired by WordPress:
<?php
function sc_sdc_callback() {
// do something
}
add_action( 'init', 'sc_sdc_callback' );
Example on how to add an action to an action hook
As an example on how to add an action to an action hook, let us look create a simple plugin that will add a custom message to the footer section of your website.
Create an empty php file named aca-sytech-plugin.php or whatever name you want, as long as it has a .php extension. Copy the following code into the file and save it. You can personalise the example plugin by changing the header information.
<?php
/*
Plugin Name: Sytech Footer Message
Version: 1.0
Description: Adds a simple text to the footer.
Author: Sydney Chako
Author URI: https://sytech.co.zw
Plugin URI: https://sytech.co.zw/footer-message/
*/
add_action('wp_footer', 'sc_sfm_footer_message', 50);
function sc_sfm_footer_message(){
echo 'Hi, I am now a cool plugin developer';
}
?>
- After saving your plugin file, upload it to the wp-content/plugins folder on your server using FTP.
- Go to your WordPress installed plugins on admin panel. If everything when well, you should now see your plugin listed among other plugins.
- Activate it.
- Then visit any page on your site, and you will see the message displayed in the footer as follows:
Now let us dissect the plugin code and see how it works:
- the plugin hooks up the
sc_sfm_footer_message()
function as an action to be executed when thewp_footer
action hook is fired. - first parameter of the
add_action
function is the name of the action hook,wp_footer
, we are targeting. - second parameter is a callback to your custom function (sc_sfm_footer_message).
- third parameter is the priority (50). Changing this number to 1 would cause the action to be called earlier.
- this action will execute every time the
wp_footer
hook is fired, ie, every time the footer is rendered.