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.
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.
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 wp_head and wp_footer. 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 wp_head and wp_footer 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.
For this article I will use wp_footer 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.
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.
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 wp_footer.
Let’s see how we can assign our custom function to wordpress hook. Its pretty simple just one line of code.
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.
As per me its pretty simple concept all you just need to do is find the appropriate hooks from the wordpress library.
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.
Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.
View all posts by: Avinash