Wordpress : Custom Action Hook in Action

It's only fair to share...Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponEmail this to someone

There are two types of hook in WordPress, filter and action. And in this occasion I will try to give examples about Actions hook.

According to codex.wordpress.org:

Actions: 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.

You can find more complete explanation about this hooks inĀ http://codex.wordpress.org/Plugin_API and right now I only give simple example to create custom hook that would give us benefit if we want to build wordpress plugin.

1. Initialize Our Hook

You can put this code in any of your plugin files

[php]
function my_first_hook(){
// initialize hook
do_action(‘my_act_first_hook’);
}
[/php]

2. Adding Action

[php]
function my_first_action(){
echo ‘This is my first hook’;
}
add_action(‘my_act_first_hook’,’my_first_action’,5);
[/php]

remember : ‘5’ at the end of the add_action is priority. 5 mean lower than 4 to execute and the default value is 10

3. Execute it

Now, we only need to call our function from the first Step in every your php files

[php]
my_first_hook();
[/php]

This function will produce

This is my first hook

That’s all we’re finish. “Really? how about if I want to append other text to my hook?”. All you need is only create another function and call it via add_action. Here it is how to do this :

[php]
function append_another_text(){
echo ‘. Append new text’;
}
add_action(‘my_act_first_hook’,’append_another_text’,6);
[/php]

Code above will produce

This is my first hook. Append new text

See the ‘6’? change it to lower number than 5 and your text will appear before the original one. “Cool, But how about if I want to pass argument into this hook?”. Good question, all you need is only change your code a bit. here it is

[php]
function my_first_hook($arg){
do_action(‘my_act_first_hook’,$arg);
}
function my_first_action($arg){
echo ‘This is my first hook with ‘.$arg;
}
add_action(‘my_act_first_hook’,’my_first_action’,5,1);
[/php]

and call it to your php files

[php]
my_first_hook(‘Argument’);
[/php]

And the code above should produce

This is my first hook with Argument

Not too complicated right?.

“How about if i want to completely remove the original text and change it with the new one?”.

There is also answer about that one. You only need to call remove_action. Here it is how to do this.

1. Remove previous action

[php]
function remove_text(){
remove_action(‘my_act_first_hook’,’my_first_action’);
}
add_action(‘init’,’remove_text’);
[/php]

2. Create your new brand new function

[php]
function brand_new_text_action(){
echo ‘Brand new text’;
}
add_action(‘my_act_first_hook’,’brand_new_text_action’);
[/php]

That’s it, want to see real action in form of plugin? you can download it hereĀ Action Hook in Action | size : 4.75 kB

Thank You, hope this article is usefull for us

It's only fair to share...Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponEmail this to someone