Using K2 Hooks

K2 is a great WordPress theme, and one of it’s strongest features is it’s ease of customization. However, if you want to add anything other than just stylesheet changes — such as Google Analytics, or some adsense code in a place other than the sidebar — you still have to edit the main template, giving you something to remember every time you update to the next version of K2.

Or do you?

It turns out that you don’t, now that K2 has added hooks to their main templates.

If you use K2, you may know that you can create a custom style by placing a folder, with a CSS stylesheet in it, in the styles folder in the K2 directory. You can also place a file called functions.php in this folder, and K2 will check there to see if you are calling any functions to be run at any of the hooks.

The hooks themselves can all be found in the header.php and footer.php files in the main K2 directory. They are located in the primary spots you might want to run some custom code… probably to place advertising, but you could use them for anything. They are called:

  • template_body_top
  • template_before_header
  • template_header
  • template_before_content
  • template_after_content
  • template_before_footer
  • template_footer

They’re located right where their names would suggest — for exact context you should take a look at header.php and footer.php to see for yourself.

So, lets say you’d like to include the google analytics code in the footer of every page, but you’d like to do this without actually editing the k2 source files… after all, if you do that you’ll need to edit the files every time you upgrade K2. K2 Hooks to the rescue.

  1. Create the functions.php file in your style’s directory. In other words, if your style is called “superfly”, you’ll create it in ./wp_content/themes/k2/styles/superfly
  2. Create a function called googleanalytics (of course, you can call it anything… george or ian or xyxxy will work, too). It will look something like:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    function google_analytics() {
    $ga_str = < <<EOD
    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
     
    <script type="text/javascript">
    var pageTracker = _gat._getTracker("UA-77047-1");
    pageTracker._initData();
    pageTracker._trackPageview();
    </script>
    EOD;
    echo $ga_str;
    }
  3. Finally, you’ll add a function call underneath; this is the magic that makes the function you just declared happen right in the spot you want it to. We’ll put it in the template_footer hook:
    1
    
    add_action('template_before_footer', 'google_analytics');

    All you did there was tell K2 which hook to use (template_footer, in this case) and which function to call there; that function being the google_analytics() function you just declared. And that function, in turn, simply spits out a block of text; your google analytics script, or ad script, or anything else you might have wanted to add.

  4. That it. You’re done.
Zemanta Pixie