Understanding add_action() in WordPress

TRY TO FIGURE OUT WHAT HAPPENED HERE!!

I Created simple function without arguments and let us extract the structure of it in wordpress.

function _hello_world(){
echo ‘hello world’;
}
add_action(‘showme’, ‘_hello_world’);

do_action(‘showme’);

Output: ‘hello world’

//THE MULTI-DIMENTIONAL ARRAY STRUCTURE OF THE CODE: using print_r($GLOBALS);

//[showme] => Array ( [10] => Array ( [_hello_world] => Array ( [function] => _hello_world [accepted_args] => 1 ) ) )

//IF YOU ARE FAMILIAR WITH ARRAY YOU WILL FIGURE OUT WHAT WILL HAPPEN ONCE YOU ADD ANOTHER ACTION IN [showme] = array variables that hold the multi-dimentional data. IF NOT REVIEW WHAT IS MULTI-DIMENTIONAL ARRAY 🙂

function _hello_world($a,$b,$c){

echo $a. $b. $c;
}

add_action(‘showme’, ‘_hello_world’, 1,3);

do_action(‘showme’, ‘Letter a ‘, ‘Letter b ‘, ‘Letter c ‘);

Output: Letter a Letter b Letter c

//THE MULTI-DIMENTIONAL ARRAY STRUCTURE OF THE CODE: using print_r($GLOBALS);

//[showme] => Array ( [1] => Array ( [_hello_world] => Array ( [function] => _hello_world [accepted_args] => 3 ) ) )

//OKAY LET US ADD MORE IN ROOT ARRAY NAME [showme]

function _hello_world(){
echo ‘hello world’;
}
add_action(‘showme’, ‘_hello_world’);

function _hello_world2(){
echo ‘hello world2’;
}

add_action(‘showme’, ‘_hello_world2’);

function _hello_world3(){
echo ‘hello world3’;
}

add_action(‘showme’, ‘_hello_world3’);

//THIS WILL RUN THE 3 FUNCTIONS UNDER showme.

do_action(‘showme’, ‘_hello_world2’, ‘_hello_world3’);

OUTPUT:
hello world
hello world2
hello world3

EXPLANATION:

$hook: is the root name of an array.
$function_to_add: the name of your function in example above _hello_world.
$priority: 1 default 10;
$arguments: 3 the $a, $b, $c.

ACCORDING TO WORDPRESS CODEX SITE:

Parameters:

$hook
(string) (required) The name of the action to which $function_to_add is hooked. (See Plugin API/Action Reference for a list of action hooks). Can also be the name of an action inside a theme or plugin file, or the special tag “all”, in which case the function will be called for all hooks.
Default: None

$function_to_add
(callback) (required) The name of the function you wish to be hooked.

Default: None
$priority
(int) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
Default: 10

$accepted_args
(int) (optional) The number of arguments the hooked function accepts. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID of the requested comment.
Default: 1

TRY TO REVIEW USING OBJECT OR CLASS IN THIS LINK
source: https://codex.wordpress.org/Function_Reference/add_action

ALSO READ HERE: IN PHP MANUAL ABOUT call_user_func()

Advertisements
This entry was posted in WORDPRESS and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s