Most of the people may gone through many article on filter or action hook, but still can’t get concept on hook of WordPress , then you are on the best place to learn.
Here i am not going to illustrate about basic theoritical meaning of hooks, just i will provide simple demonstration on hooks.
Before starting i hope you guys are clear on working of function of php.
Hooks are actually comes on action as calling and called function.
Lets say : Example1
On functions.php file you had defined a function called sum.
Function add_function ( $input_a, $input_b ) { $total = sum ($input_a, $input_b); return $total; }
Normally to called this function we used
add_function (5,6); //this is calling function 5 and 6 are arguments.
_________
If you understand how the above code execute then there is no any worry, hooks on WordPress also works on the same way.
Somewhere on WordPress templates [take header.php ] you had added calling function as:
do_action ('sum_the_total', $argument_1, $argument_2 ); apply_filters ('sum_the_total', $argument_1, $argument_2 )
Here sum_the_total is the name of hooks, through which we can run many function at a time with given priority on action hooks/filter_hooks
do_action () and apply_filters () are calling function which functions as above example1 calling functions:
add_function ( 5, 6);
Now we need to defined function on functions.php template, lets say we declared same function as on example1
Example2:
Here $input_a and input_b are parameter which carries value hold by arguments: $argument_1 and $argument_2
Function add_function ( $input_a, $input_b ) { $total = sum ($input_a, $input_b); return $total; } add_action ( 'sum_the_total', 'add_function', 12); add_filter ( 'sum_the_total', 'add_function', 12);
similarly let’s say:
Function add_function_2 ( $input_a, $input_b ) { $total = sum ($input_a, $input_b); return $total; } add_action ( 'sum_the_total', 'add_function_2', 13); add_filter ( 'sum_the_total', 'add_function', 12);
Here “sum_the_total” is name of hooks which is defined on do_action which will hooks function add_function and add_function_2. Then execute them as per their priority given:
There on example2 add_function will execute first due to its priority = 12 . [The function with less priority will execute first. ]
Output:
if on header.php file $argument_1 = 1; $argument_2 = 2; echo do_action ('sum_the_total', $argument_1, $argument_2 );
Output will be :
3</pre>
For reference:
Meaning of hooks by wordpress.org:
https://developer.wordpress.org/reference/functions/do_action/
https://developer.wordpress.org/reference/functions/add_action/
Have a look on difference between filter and action hooks here:
http://zellwk.com/blog/wordpress-actions-and-filters/
http://ottopress.com/2011/actions-and-filters-are-not-the-same-thing/
Note: If you require any information or have any question or queries or you have better soution than this then please feel free to contact me at adhsushil7@gmail.com
Thanks