Action is a hook function that allows developers to add their code inside the website. It provides a solution for running the function at a specific point in the execution of WordPress Core, Plugins, and Themes.
WordPress uses certain functions at different points throughout the core. It has predefined hooks and actions that allow developers to add their codes to work simultaneously. WordPress allows complete flexibility over the website.
You have to follow these two steps to create an action function.
The first is to create a callback function. It will run when the action inside is hooked.
To create a callback function you have to access the functions.php file and create a normal, callable function. And the parameters should be defined by the action that you are hooking.
The second is to assign the callback function. Add callback into action with add_action function. It is known as hooking. It tells the action to run your callback function when the action is run.
Flow of code:
- crate a callback function.
- hook it with add action
- Use the string $tag to name the function which you are hooking.
- Use callable $function_to_add to name the callable function.
The example below will run wporg_callback()
when the init
hook is executed:
function wporg_callback() {
// do something
}
add_action( 'init', 'wporg_callback' );