1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[HOW TO] Add 'in-content' AdSense code in WordPress via shortcode

Discussion in 'Black Hat SEO' started by bluehatface, Jan 7, 2014.

  1. bluehatface

    bluehatface Regular Member

    Joined:
    Oct 19, 2013
    Messages:
    232
    Likes Received:
    98
    Location:
    Here
    I've been messing around trying different layouts for my AdSense ads on one of my sites. I wanted to add a rectangle in-content as I had previously just had a leaderboard above the content.

    My website has various types of posts, so simply sticking the ad before the first paragraph just wouldn't cut it, so I added a little shortcode so I can choose which posts and which location to add it.

    For those who can't edit PHP, CSS and HTML, I'thought I'd give a little how-to here to help you get it working. Sure you can probably get a plugin, but who knows what bloat and sneaky tricks they're up to! (Apologies to those who do).

    Doing it as a shortcode gives you maximum flexibility as to where it will be placed, helping reduce ad-blindness, but will take longer to set up.

    Firstly... Take a copy of the functions.php file in your theme's folder... Always edit offline, and upload via FTP. Never edit via the editor in WordPress, if you make an error, you'll brick your site and will have to upload a new functions.php file via FTP anyway.

    Here's how we do a shortcode. Add it to the functions.php file...

    Code:
    function shortCodeExample() {
            return 'STUFF TO INSERT';
    }
    add_shortcode('foo', 'shortCodeExample');
    In your WordPress post, adding the shortcode [foo] will add STUFF TO INSERT in it's place. We want to add the AdSense code now so get the code from the AdSense website (This is based on the new asynchronous ad type)...


    Code:
    // Adsense In-Content Shortcode
    function adsenseInContentShortcode() 
    {
        return '
              [B]<div id="adsenseInContent" style="display:block;float:left;>[/B]
              <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
              <!-- In Content -->
                    <ins class="adsbygoogle"
                        style="display:inline-block;width:300px;height:250px[B];vertical-align:top;margin-right:20px[/B]"
                        data-ad-client="ca-pub-xxxx"
                        data-ad-slot="xxxx">
                    </ins>
                <script>
                        (adsbygoogle = window.adsbygoogle || []).push({});
                </script>
                [B]</div>[/B] ';
    }
    add_shortcode('adsenseInContent', 'adsenseInContentShortcode');
    Make sure you add all the AdSense code between the single quotes, and make sure that you use single quotes. I've added some extra code above in bold to the code supplied by AdSense. This is to ensure the formatting works properly, and the text wraps around. I've blanked out my adsense code (obviously) but if you make yours look similar, it should all work fine.

    Simply use the shortcode [adsenseInContent] and voilĂ !!
     
    • Thanks Thanks x 1
    Last edited: Jan 7, 2014