Genesis simple hooks & Google codes

 

In this article I will tell you how using Genesis simple hooks you can add the codes from Google analytics and/or adsense to the website running on Genesis framework for WordPress.

First thing first, what are Genesis simple hooks?

As said on Studiopress website “A hook is a piece of code written into a theme, which allows you to attach content to the theme itself.” (Ref – Studiopress)

 

Art16_1

 

So what does that mean for a site developer? It means that in Genesis theme there are functions created to which you can add in your piece of code. These functions are called hooks and are used to display/execute your piece of code at different location of the theme layout. Overall there are 45+ such hooks built into the Genesis theme. For example, you can execute you piece of code just after the header by adding you code to the hook genesis_after_header or use genesis_after _content to execute the piece of code immediately after the content. To refer the entire list of hooks click here.

The Genesis Simple Hooks plugin was one of the first plugins that I downloaded. The plugin makes it simple to use the simple hooks. It is the easiest way to add in your code without editing the theme’s function.php. The work that’s done by a hook can also be accomplished by editing the function.php and adding in the entire new code for defining the new function. But think, if you miss one semi-colon in function.php and your entire site will get messed up. This means, managing the code easy although by intruding the function.php and that is exactly what Simple Hooks do.

So, let`s see how to hook a Google code to the site using simple hooks.

Adding Google Analytics code  

Setting up the Google analytic account is what most of the website owner do when launching a new website. Once you set the analytic, you are given the script code which needs to be inserted into you site code so that Google can track.

Open up the Genesis simple hook plugin and go to the hook genesis_after. Add the analytic code and save it.

Art16_2
That is it, analytics installed. The code will execute just before the body tag, which is one of the best place to have the analytic code on the site.

Adding Google Ad code

Every blogger wants to generate revenue from the developed site and nothing is better than placing a Google Ad on the site.  Let`s say, the ad should be displayed on the single article page just after the article title and before the start of the content. To achieve this we will have to use the genesis_before_post_content hook.

Go to the Simple hook plugin and search of genesis_before_post_content hook. Add the Google ad code in the text box. You will not achieve what you want just by adding the ad code, it will require a few lines of PHP code and conditions too. To display the ad only on the single article page and not on the home page the !is_home conditional tag needs to be used. Check below the PHP code that`s been added around the Google Ad code. Once done with the coding, select the Execute Shortcodes on this hook and Execute PHP on this hook, in order to execute the added PHP code.

Art16_5

Now you can see the Google Ad just before the start of the content on all of your sites articles. Something like below…

 Art16_4

 

Note, there are many conditions that can be used like the one we used, is_home. For more such conditional tag click here.

Speak Your Mind

*