WordPress Action Hooks Explained

  • Avinash
  • 3
  • Jul 29, 2012
  • Tutorial

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.

  1. Insert some code in header or Footer
  2. New post or page published
  3. Comment edited or Removed
  4. Any user is deleted from the site
  5. 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 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.

How to use 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.

  1. Create a function which contain your custom code.
  2. 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.

function inject_ga_code() { echo ""; }
function inject_ga_code()
{
  echo "<script type="text/javascript">
var _gaq=_gaq||[];_gaq.push(['_setAccount','UA-16574115-1']);
_gaq.push(['_trackPageview']);
(function(){var ga=document.createElement('script');ga.type='text/javascript';
ga.async=true;
ga.src=('https:'==document.location.protocol?'https://ssl':'http://www')+
'.google-analytics.com/ga.js';
var s=document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga,s);})();</script>"
;
}

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.

add_action('wp_head', 'inject_ga_code');
add_action('wp_head', 'inject_ga_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.

add_action('wp_head', 'inject_ga_code'); function inject_ga_code() { echo ""; }
add_action('wp_head', 'inject_ga_code');

function inject_ga_code()
{
  echo "<script type="text/javascript">
var _gaq=_gaq||[];_gaq.push(['_setAccount','UA-16574115-1']);
_gaq.push(['_trackPageview']);
(function(){var ga=document.createElement('script');ga.type='text/javascript';
ga.async=true;
ga.src=('https:'==document.location.protocol?'https://ssl':'http://www')+
'.google-analytics.com/ga.js';
var s=document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga,s);})();</script>"
;
}

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.

Related Posts

Written by Avinash

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: