WordPress Quick Tip: Embed Google AdSense Shortcode

  • follow us in feedly
Published October 19, 2013 by Brad Knutson
Google AdSense WordPress Shortcode

It’s not uncommon for bloggers to put ads between paragraphs of their post. For experienced bloggers, adding in the Google AdSense code to your post isn’t a big deal, but what about someone who isn’t comfortable with any code?

A client of mine asked how they could embed their ads into the body of their posts, rather than the footer or sidebar like they had been. They didn’t feel comfortable working in code, and wanted a really simple solution.

It occurred to me that this could be accomplished with a really simple shortcode. Everything could be automated, and the blogger would just need to remember to enter a simple shortcode where they wanted their Ads to show up.

The Shortcode

Add the following function to your theme’s functions.php file. Replace the AdSense code with your own code so your add shows up.

function showads() {
    return '<script type="text/javascript"><!--
                google_ad_client = "pub-3637220125174754";
                google_ad_slot = "4668915978";
                google_ad_width = 468;
                google_ad_height = 60;
                //-->
                </script>
                <script type="text/javascript"
                src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                </script>';
}
add_shortcode('adsense', 'showads');

Now, all the blogger has to do is use the new shortcode you created.

Usage

[adsense]

That’s it!

Credit for shortcode goes to WpRecipes.

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date

Subscribe

Topics

See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

One thought on “WordPress Quick Tip: Embed Google AdSense Shortcode

  1. jeromz

    Hello

    I’m not an expert and I need your help. I added in my founction.php the below code

    function google_adsense() {
    return ‘

    (adsbygoogle = window.adsbygoogle || []).push({});
    ‘;
    }
    add_shortcode(‘adsenseyu2′, ‘google_adsense’);

    function google_adsense2() {
    return ‘

    ‘;
    }
    add_shortcode(‘adsenseyu1′, ‘google_adsense2’);

    the result is a blank page but when I remove the first or the second function, it works correctly
    I need your help to understand what’s wrong with this code, I’m sure I forget something

    please help
    thks in advance

    Reply

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">