Hi All, Here now I am about to move one step ahead in WordPress Hook Article Series. Earlier what we have seen is Introduction to the Hooks in wordpress. In this article I am going to cover one of the Hook type in wordpress which is Action Hooks.
What are Action Hooks?
Before saying anything let’s take some word from official documentation.
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.
As definition says, Action Hooks are piece of code which we can execute on specific event occurs in wordpress.
Let me give you some examples where we can hook our own functions using action hooks.
- Insert some code in header or Footer
- New post or page published
- Comment edited or Removed
- Any user is deleted from the site
- Any user login to the system.
These are just an examples full list of Action Hooks can be found here.
So now in this article we will see how we can execute our own code using available hooks in wordpress. The most used action hooks in wordpress are [code]wp_head[/code] and [code]wp_footer[/code]. You can find these hooks any wordpress theme. In fact lots of plugin depends on these hooks which automatically place code for you in header or footer section.
For now I assume that we all know that [code]wp_head[/code] and [code]wp_footer[/code] are required to be placed in theme. But how we can use the same. So in next section we will see how we can use the wordpress action hooks.
How to use WordPress Action Hooks?
For this article I will use [code]wp_footer[/code] hook and show you how we can use the wordpress action hooks. I will show you how we can inject some code in footer section using the wp_footer action hook.
As per me there are only two steps required to use the wordpress hook which are as below.
- Create a function which contain your custom code.
- Assign your function to appropriate wordpress hook.
I am going to inject the Google Analytics code to the footer of the page using wp_footer action hook.
As per the above steps, First let’s create a function which output the Google Analytic code. Just have a look at below code block for the same.
[cc lang=”php”]
function inject_ga_code()
{
echo ““;
}
[/cc]
OK, so now we have created a function which is able to echo the google analytics code. Now next we have to assign this function to appropriate hook function, in our case it is [code]wp_footer[/code].
Let’s see how we can assign our custom function to wordpress hook. Its pretty simple just one line of code.
[cc lang=”php”]
add_action(‘wp_head’, ‘inject_ga_code’);
[/cc]
So finally we have our code is ready which able to include the Google Analytics code. Let’s see how our final code look like.
[cc lang=”php”]
add_action(‘wp_head’, ‘inject_ga_code’);
function inject_ga_code()
{
echo ““;
}
[/cc]
As per me its pretty simple concept all you just need to do is find the appropriate hooks from the wordpress library.
Conclusion
In a nutshell Action hooks are the places where we can inject our own code which will execute on certain events in the wordpress. Please check that here events is in bold face which includes post creation, user creation, comments creation, etc..
In next article we will see WordPress Filter Hooks in details. Subscribe to our RSS Feed by email to get all the updates directly to your email and yes don’t forget to Follow us on Twitter and Like us on Facebook.
[…] previous article we have covered the Action Hooks in WordPress. In this article we are about to explore the Filter Hooks in […]
Hi, i think you mean
add_action('wp_footer', 'inject_ga_code');
instead ofadd_action('wp_head', 'inject_ga_code');
.Thanks for sharing!
Hi, i think you mean
add_action('wp_footer', 'inject_ga_code');
instead ofadd_action('wp_head', 'inject_ga_code');
.Thanks for sharing!
[…] A Responsive WordPress Theme Is More Than Install-And-Go WordPress Filter Hooks Explained WordPress Action Hooks Explained Vous devriez aussi lire ces billets Webdesign Express : AJAX, bouton, cache et image […]