Adding Adsense Ads After 1st Paragraph

I'm trying to add an adsense unit that will be aligned left (with the post text wrapping around it) after the 1st paragraph on post pages on my website PantrySpa.com.

I have added the following to adsense-inpost.php:

<?php
$paragraphAfter= 1; // Insert Ad after 5 paragraphs
$ad = '<p id="adsense-inpost"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- PantrySpa Sidebar 300x600 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:600px"
data-ad-client="ca-pub-7412812612421318"
data-ad-slot="7990461632"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</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>";
}
?>

And I have added the following to the bottom of functions.php
//* Add Adsense-Inpost.php
add_action('genesis_before_post_content', 'include_adsense_inpost');
function include_adsense_inpost() {
if ( is_single() ) {
remove_action('genesis_post_content', 'genesis_do_post_content');
require_once(CHILD_DIR . '/adsense-inpost.php');
}
}

However, the adsense unit is not showing up on the post pages. Please let me know how to fix this. I have granted support access to PantrySpa.com via the WPMU plugin, in case that is helpful...

Thanks!!!

  • Ash

    Hello @d-invent

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    Please try the following code:

    add_filter( 'the_content', 'addsense_code' );
    function addsense_code( $content ) {
    	$ad = 'Your adsense code here';
    	$content = explode( "</p>", $content );
    	$content[0] = $content[0] . $ad;
    	$content = implode( "</p>", $content );
    	return $content;
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • d-invent

    Thanks... that partially worked, but the text from the article is not wrapping around the text. I tried to modify it by adding <p style="text-align: left;"> (as shown below), but that did not seem to help. How can I make the text of the article wrap around the ad unit so that there isn't so much empty white space to the right of the ad unit?

    add_filter( 'the_content', 'addsense_code' );
    function addsense_code( $content ) {
    $ad = '<p style="text-align: left;"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- PantrySpa Sidebar 300x600 -->
    <ins class="adsbygoogle"
    style="display:inline-block;width:300px;height:600px"
    data-ad-client="ca-pub-7412812612421318"
    data-ad-slot="7990461632"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script></p>';
    $content = explode( "</p>", $content );
    $content[0] = $content[0] . $ad;
    $content = implode( "</p>", $content );
    return $content;
    }

  • aristath

    Hello there @d-invent, I hope you're well today!

    You could try using this:
    <p style="text-align: left; float: left;">

    Let me know if that works!

    Also have you seen our In-Post Ads plugin? https://premium.wpmudev.org/project/in-post-ads/
    You could use that instead of your custom code and then simply write some custom CSS (using float: left:wink: to make the text wrap around it.

    I hope that helps!

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.