WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

May 13 2011

How to Add Google Adsense After the First (or Second) Paragraph of a Post in Genesis

This question was raised in the StudioPress forums: How to Add Google Adsense After the First (or Second) Paragraph of a Post in Genesis? So here's how I did it initially:

[php]<?php
add_filter( 'the_content', 'my_add_after_first_p', 20 );
function my_add_after_first_p( $content ) {
$ad = '</p><p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxxxx";
/* 468x60, created 10/4/09 */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>';
$content = preg_replace( "/</p>/", $ad, $content, 1 );
return $content;
}
?> [/php]

However, I then came across another method (on @wpbeginner who grab it from somewhere else), which I like better, after the individual wanted it after the 2nd paragraph. So copy the following code and place it in its own php file called adsense.php (or download it here: [download id="9"]).

[php]<?php
$paragraphAfter= 1; //display after the first paragraph, change to 2 for 2nd paragraph
$ad = '</p><p style="text-align: center;">
<script type="text/javascript"><!--google_ad_client = "pub-xxxxxxxxxxxxxxxxxx"; /* 468x60, created 10/4/09 */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></p>';
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++ ) {
if ($i == $paragraphAfter)
echo $ad;

echo $content[$i] . "</p>";
}
?>
[/php]

Then in functions.php add the following:
[php]
add_action('genesis_before_post_content', 'include_adsense');
function include_adsense() {
if ( is_single() )
require_once(CHILD_DIR . '/adsense.php');
}
[/php]

Written by Travis Smith · Categorized: Genesis, Tutorials

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Adam W. Warner says

    May 14, 2011 at 9:24 am

    Great advice, thanks for posting (and reminding me that I was going to experiment with AdSense on one of my sites again). I like the second method, seems more clean even though it’s a two-step process.

    Reply
  2. Ian says

    August 30, 2011 at 8:09 am

    I’m actually using Thesis right now, but have been considering Genesis.

    How easy would it be to include a check box on the edit post/page screen to only allow this on posts of your choosing?

    Reply
    • Travis Smith says

      August 30, 2011 at 8:57 am

      With a checkbox, it is as easy (or difficult) as adding a metabox to the code with a single conditional statement. With the custom metabox script, it is quite easy.

      Reply
  3. Detectiva says

    September 26, 2011 at 6:39 pm

    Thank you but it does not work on my site.

    When I applied the second option, (after uploading the adsense.php and adding that code into function.php) it simple made my post disappear.
    Here is the blog I am talking about, http://detectiva.com/blog/english-to-spanish/good-morning-in-spanish/ and please get back to me asap, thank you a million. 😉

    Reply
    • Travis Smith says

      December 14, 2011 at 1:39 am

      It appears that you’ve got it working???

      Reply
      • Travis Smith says

        April 2, 2012 at 8:18 am

        Please refer to the plugin version of this code: http://wordpress.org/extend/plugins/gravity-forms-terms-of-service-field/

        Reply
  4. Lisa says

    November 26, 2011 at 1:01 pm

    hi guys, this code is ignoring [/caption] therefore jumping inside captions for my post images. Apparently those words qualify as first paragraph. Any ideas?

    Reply
  5. Jabran says

    December 9, 2011 at 4:58 pm

    Thanks for the wonderful tip. There is however a mistake.

    For the second tip:
    In line 10 of adsense.php code, the ‘ should be replaced by ‘
    (Just a change in the placement of apostrophe)

    Reply
  6. Jim Rush says

    February 21, 2013 at 4:32 pm

    With a few adjustments to the code it works, thank you!

    Reply
  7. Nguyen Dat Tai says

    May 17, 2013 at 12:41 am

    How to Add Google Adsense After the First in Genesis 2.0?

    Thanks!

    Reply
  8. Michael says

    November 25, 2013 at 6:38 pm

    Thanks a lot Travis for your code snippet! I have modified your snippet to work with a regular WordPress install and added in multiple ads. Maybe this helps somebody: http://www.schurpf.com/displaying-ads-after-first-paragraph-with-wordpress-or-after-any-paragraph-you-wish/

    Thanks for posting this!

    Reply
  9. manish kapoor says

    February 18, 2015 at 1:27 pm

    I am struggling a lot since last 3 days to add adsense between posts on my website http://www.funnyquotes4u.net. I am using DIVI theme. Can you please help me out?

    Reply
  10. pakown says

    February 19, 2016 at 10:55 pm

    This has solved my problem…thanks

    Reply
  11. Biplab Acharjee says

    January 11, 2017 at 11:13 pm

    Thanks for creating this post Travis Smith. I have got my solution form your post. Its working for me.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in