Google Adsense Section Targeting With Thesis Theme For WordPress

Crazy this! When you mix the power of WordPress Hooks, Thesis theme and Google Adsense section targeting, you get highly target ads. Add these simple lines of code into your custom_functions.php file in the Thesis theme and you are all set to go.

What this does is it tells the adsense bot to ignore everything other than the post content which really is the meat of the blog. And the Adsense bot is intelligent enough to override section targeting if it wishes so you don’t lose a thing like the blog title and SEO etc. focused on Adsense targeting.

//adsense targeting functions
function open_adsense_targeting()
{
?>
<!-- google_ad_section_end -->
<!-- google_ad_section_start -->
<?php
}
function close_adsense_targeting()
{
?>
<!-- google_ad_section_end -->
<!-- google_ad_section_start(weight=ignore) -->
<?php
}
function ignore_adsense_targeting()
{
?>
<!-- google_ad_section_start(weight=ignore) -->
<?php
}
function end_adsense_targeting()
{
?>
<!-- google_ad_section_end -->
<?php
}
add_action('thesis_hook_before_headline','open_adsense_targeting','10' );
add_action('thesis_hook_after_post', 'close_adsense_targeting','10');
add_action('thesis_hook_before_html', 'ignore_adsense_targeting','10');
add_action('thesis_hook_after_html', 'end_adsense_targeting','10');

I got the original idea from Graywolf’s SEO Blog which lacked the ignore section of Adsense Section Targeting

Comments are closed.

45 Comments

  1. Do you have an example of what a Thesis page page looks like when that code is applied?

    Also, I only show part of each post on the home page, with a “Click here to continue” to a page with the full article. How do I modify your code so that Adsense only appears on the full article page, not on the home page?

    TIA

    Info
  2. The above code does not alter the look of the site.

    This site (Convertica.com) is an example of this code’s implementation.

    This code works without any modification whether you show full-articles or teasers.

  3. By “what the page looks like” I meant I wanted to see the Adsense sitting on the page.

    But I must be missing something, because I can’t see any Google Adsense on this page at all, or on your home page.

    Info
  4. There’s at least one on every page. For example the ad in the footer of this page. If you can see it then something may be wrong with my site because I can see all the ads. If you can send me a screenshot at varun21 at gmail dot com.

  5. OK, my apologies, I did miss that image ad in the footer.

    So where do you put your own unique Adsense code.

    Info
  6. I got the same results as Sacrificial Lamb and would repeat the same question. Where do you put your own unique code?

  7. Can we get instructions as to where the unique Adsense Code goes?
    Thanks

  8. It goes at the end of the custom_functions.php file in Thesis theme. Here’s the new code I use.

    //adsense targeting functions
    function open_adsense_targeting()
    	{
    	 ?>
    	 <!-- google_ad_section_end -->
    	<!-- google_ad_section_start -->
    	<?php
    	}
    
    function close_adsense_targeting()
    	{
    		 ?>
    	<!-- google_ad_section_end -->
    	<!-- google_ad_section_start(weight=ignore) -->
    	<?php
    	}
    
    function ignore_adsense_targeting()
    	{
    	 ?>
    	 <!-- google_ad_section_start(weight=ignore) -->
    <?php
    	}
    
    function end_adsense_targeting()
    	{
    		?>
    	<!-- google_ad_section_end -->
    	<?php
    	}
    
    add_action('thesis_hook_before_html', 'ignore_adsense_targeting','10');
    add_action('thesis_hook_before_header','open_adsense_targeting','9' );
    add_action('thesis_hook_after_header','close_adsense_targeting','8' );
    add_action('thesis_hook_before_headline','open_adsense_targeting','10' );
    add_action('thesis_hook_after_post', 'close_adsense_targeting','10');
    add_action('thesis_hook_after_html', 'end_adsense_targeting','10');
    
  9. hello how you are showing google ads in header section of thesis theme ? pls pls tell me i need that

  10. Hi there, thanks so much for this post. I am struggling with Adsense relevance as well. I use Thesis now and in the support forums, it was recommended that a similar code be saved as a file.php and then called as a function into custom_functions.php, just to be cleaner and smoother. I am *not* a coder at all, so I followed instructions. Do you have any idea if there is a way to test it, other than ads starting to look more relevant? Is there a way to know whether it is being applied and I did it correctly? 🙂
    Thanks so much!!!

  11. The only way is to look up the results in the page’s source code. If you can provide the URL of the related page that will help.

  12. Hi, sure thing. I thought my blog URL showed up when you clicked on the avatar.
    Here it is:
    http://www.prolificliving.com/blog
    I will also check the source code but would be great to hear your input as well. Thanks!

  13. I did check that already but found nothing. If all goes well the source should reflect sections containing text similar to “google_ad_section_start”.

  14. I see ok. Hmm. Not sure. I doubt it takes time to propagate, as it is just code lines you are looking for. So what I exactly did is: Created monetization.php which I pasted below, then added this line: include(‘monetization.php’); in the middle. Replaced with the new file.
    —monetization.php reads:
    <?php /* Monetization Functions */

    // After Header
    function after_header_ads() {
    $ad = '
    ‘;

    if (!is_404() && !is_search() && !is_page(167) && !is_page(290) && !is_page(291) && !is_page(447) && !is_page(727))
    echo $ad, “\n”;
    }
    add_action(‘thesis_hook_after_title’, ‘after_header_ads’);

    // Targeting
    function ad_target_begin() {
    echo ‘‘, “\n”;
    }
    function ad_target_end() {
    echo ‘‘, “\n”;
    }
    function ad_ignore_begin() {
    echo ‘‘, “\n”;
    }
    add_action(‘thesis_hook_before_html’, ‘ad_ignore_begin’);
    add_action(‘thesis_hook_before_post’, ‘ad_target_end’);
    add_action(‘thesis_hook_before_post’, ‘ad_target_begin’);
    add_action(‘thesis_hook_after_post’, ‘ad_target_end’);
    add_action(‘thesis_hook_after_post’, ‘ad_ignore_begin’);
    add_action(‘thesis_hook_after_html’, ‘ad_target_end’);

    ?>
    ================
    Do I need to have hooks installed already on thesis? I did not read that and I haven’t yet installed them. I thought it is just calling innate functions to thesis.
    Sorry about this, and thanks again. I know it’s hard to imagine all that could be going wrong here from the info I am sending but nonetheless, thank you!

  15. I don’t know how putting this code into a separate file will make it smoother… I guess it’s personal preference. I keep all my code in custom_functions.php. That eliminates one element of errors. For testing try putting the following code into custom_functions.php and remove any references to monetisation.php. Once this works we can then move this code to monetisation.php.

    function after_header_ads() {
    $ad = '';
    
    if (!is_404() && !is_search() && !is_page(167) && !is_page(290) && !is_page(291) && !is_page(447) && !is_page(727))
    echo $ad, '\n';
    }
    add_action('thesis_hook_after_title', 'after_header_ads');
    
    //adsense targeting functions
    function open_adsense_targeting()
    	{
    	 ?>
    	 
    	
    	< ?php
    	}
    
    function close_adsense_targeting()
    	{
    		 ?>
    	
    	
    	< ?php
    	}
    
    function ignore_adsense_targeting()
    	{
    	 ?>
    	 
    < ?php
    	}
    
    function end_adsense_targeting()
    	{
    		?>
    	
    	< ?php
    	}
    
    add_action('thesis_hook_before_html', 'ignore_adsense_targeting','10');
    add_action('thesis_hook_before_header','open_adsense_targeting','9' );
    add_action('thesis_hook_after_header','close_adsense_targeting','8' );
    add_action('thesis_hook_before_headline','open_adsense_targeting','10' );
    add_action('thesis_hook_after_post', 'close_adsense_targeting','10');
    add_action('thesis_hook_after_html', 'end_adsense_targeting','10');
  16. And by the way… as you correctly said hooks are just wordpress or theme functions. They can’t be installed.

  17. Shivanand, thank you for taking so much time to help me. You are so kind.
    I took your advice. I opened my custom_functions.php (which by the way, being novice at this, just edit in notepad and then ftp it over to overwrite the older file, and make sure the last edited date is when I edited the file). It is now done. I checked my source code on the main /blog page, don’t see any reference to google_ad_section anywhere. Below is my entire custom_functions.php code:
    ?php

    // Using hooks is absolutely the smartest, most bulletproof way to implement things like plugins,
    // custom design elements, and ads. You can add your hook calls below, and they should take the
    // following form:
    // add_action(‘thesis_hook_name’, ‘function_name’);
    // The function you name above will run at the location of the specified hook. The example
    // hook below demonstrates how you can insert Thesis’ default recent posts widget above
    // the content in Sidebar 1:
    // add_action(‘thesis_hook_before_sidebar_1’, ‘thesis_widget_recent_posts’);

    // Delete this line, including the dashes to the left, and add your hooks in its place.

    /**
    * function custom_bookmark_links() – outputs an HTML list of bookmarking links
    * NOTE: This only works when called from inside the WordPress loop!
    * SECOND NOTE: This is really just a sample function to show you how to use custom functions!
    *
    * @since 1.0
    * @global object $post
    */

    function custom_bookmark_links() {
    global $post;
    ?>

    include(‘monetization.php’);

    function after_header_ads() {
    $ad = ”;

    if (!is_404() && !is_search() && !is_page(167) && !is_page(290) && !is_page(291) && !is_page(447) && !is_page(727))
    echo $ad, ‘\n’;
    }
    add_action(‘thesis_hook_after_title’, ‘after_header_ads’);

    //adsense targeting functions
    function open_adsense_targeting()
    {
    ?>

    < ?php
    }

    add_action('thesis_hook_before_html', 'ignore_adsense_targeting','10');
    add_action('thesis_hook_before_header','open_adsense_targeting','9' );
    add_action('thesis_hook_after_header','close_adsense_targeting','8' );
    add_action('thesis_hook_before_headline','open_adsense_targeting','10' );
    add_action('thesis_hook_after_post', 'close_adsense_targeting','10');
    add_action('thesis_hook_after_html', 'end_adsense_targeting','10');

    <a rel="nofollow" href="http://delicious.com/save?url=&title=” onclick=”window.open(‘http://delicious.com/save?v=5&noui&jump=close&url=&title=’, ‘delicious’, ‘toolbar=no,width=550,height=550’); return false;” title=”Bookmark this post on del.icio.us”>Bookmark this article on Delicious

    <?php
    }

  18. It seems to me that your functions.php file is not being read at all. I figured that because the bookmark links etc. are also not showing.

    I noticed one thing. That your thesis theme directory is named “thesis_151” instead of “thesis”.

    If you like you can send me your custom_functions.php and I can check it on a demo blog for you. my email is varun21 at gmail dot com.

    I hope you have placed it at the right location i.e.
    http://www.prolificliving.com/blog/wp-content/themes/thesis/custom/custom_functions.php click this link it should throw an error instead of the “You 404’d it. Gnarly, dude.” Thesis error.

  19. Dear Shivanand, a note to say THANK YOU for all your help. I am amazed every day by the tightness of the blogging community and in gratitude to those who take the time to help me. I hope I can help you in return someday. In the meanwhile, I will promote the hell out of your blog, and you, sir, rock. Thank you for fixing my php code offline and all your time.

  20. i set up a search page using your site… but i am having problem.. problem is that it is showing “You 404’d it. Gnarly, dude.” on every search… which means its not working… please help me…

  21. Do you know how to add google adsense “ad links” under the title of the page, on the header?

  22. I can verify this code works beautifully. I’ve been using this kind of code on Thesis Theme HQ and two of my other sites for about 6 months now. This technique is widely publicised amongst the Thesis Theme Community. When recently upgrading to Thesis 1.6 I googled for this code and found it here. My site went from displaying hotel ads to very relevant ads. Check out one of my posts and see how I use Google Adsense AFTER HEADLINE. Thanks for the code Shivanand.

  23. I have setup Adsense section targeting with the help of your article. Thank you!

  24. I have a question.
    We have to add and inside our text. (Text we want to target)
    OR
    What?
    Please explain this.

  25. Not inside the text, but inside the source of the html page, such that the text that you want to target falls in between those tags.

  26. Great write-up! thanks for sharing. Just curious, is there a way to have the ads not show when viewing category menu results pages? I’ve got it working on http://www.knowhealthcare.com, but category pages are messy – in fact Google only shows limit 4 ads because they begin repeating.

  27. Great!, its pretty easy to integrate section targeting in thesis!

  28. @Noticias: You’ll want to have someone integrate Google AdSense into the Thesis theme for you and implement section targeting. You will have to contact a web designer or you can have us do this for you. Just leave a comment if you are interested and we’ll be more than happy to assist you. We have extensive experience in web design and development and have various services on offer.

  29. Hi I am intersted in adding this to my site but I am not a coder and don’t really get where is my google adsence code supposed to be added into this code also is this the only code needed or do I need to add more lines inside other files like the .css that will tell this code where I would like my google ads and what size I would like to be in that area

  30. Does anyone know how to put ads right after headline of the post on main page, rather than on individual posts? Thank you for sending an email, if you know the answer.

  31. @buzzhunt You can use the hooks to attach the ads after the headlines on the front page.

  32. @Shivanand Sharma – good to know they exist, but I could not find one that does the front page – only those which do single posts. Do you know where exactly I can find the right hook?

  33. @buzzhunt “thesis_hook_after_headline” is the hook you want to use. The function you hook must only show the ads “if(!is_single())”. You can find the complete list of hooks at DIYThemes.com homepage.

  34. Thank you for your quick reply, much appreciated!

  35. Hi Shivanand

    I Want to ask cause i’m new with thesis theme
    add_action(‘thesis_hook_before_headline’,’open_adsense_targeting’,’10’ );
    1. That “10” number is for what?
    2. If I want google ad section just on title only what should i doing?
    example :

    <h1 class=……………………….

    I hope you can help me
    Thank’s before

  36. @smartblogs That 10 is for the priority. Given that two different functions are attached to the same hook, the one with a priority of 9 would fire before the one with a priority of 10.

    We all would like to just add section targeting around the keywords only. However that’s not how Adsense works. It uses it’s own intelligence too and overrides section targeting where necessary.

  37. Hi Shivanand,
    Thanks for the useful links on Thesis. I’ve just added these 2 hooks:

    function after_content_ad() { ?>
    // INSERT YOUR AD CODE HERE
    <?php }

    add_action('thesis_hook_after_content', 'after_content_ad', 1);

    and

    function single_post_ads() {
    if (is_single()) { ?>
    // INSERT YOUR AD CODE HERE
    <?php }
    }

    add_action('thesis_hook_after_post', 'single_post_ads');

    Then, I added the “thesis_hook_after_headline” by copying both codes and replace the ‘thesis_hook_after_post’ with this. My blog disappeared!

    What did I do wrong? I’ve just asked my web host to fix it for me. Hope he won’t get mad because I’ve been tinkering with my blogs and messing it up a lot. Sigh…

  38. KittyCat: when you say you “copied both codes”, did you copy the function after_content_ad()…. and function single_post_ads()… also? If yes, then that’s what may be causing the issue. You can not have two declarations of the same function.

    Given that you have the functions as you mentioned above, all you have to do is include one additional line to get the ads to show after headline:

    add_action('thesis_hook_after_headline', 'single_post_ads');
  39. This is a good post, I stumbled across your article while looking for song downloads. Thanks for sharing, I’ll be sure to recommend this site to others.

  40. This is a great post, I stumbled across your post while looking for free downloads. Thanks for sharing, I’ll be sure to recommend this site to others.

  41. It goes at the end of the custom_functions.php file in Thesis theme. Here’s the new code I us

Comments are closed.