Add an AdSense block to a WordPress sidebar

Adsense is probably the most common contextual ad service out there. For WordPress there’s a whole bunch of plugins for installing the ad code.

In this post I’ll show the simplest and fastest way to drop an AdSense ad code into your blog’s sidebar without any extra plugins. You won’t need any plugins for this at all.

First pick a square ad block in your Adsense panel. Let’s look at the code more closely:
It loads an external library:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

A comment specifying the block size:

<!-- 250x250 -->

The adsbygoogle class is declared:

<ins class="adsbygoogle"
     style="display:inline-block;width:250px;height:250px"
     data-ad-client="ca-pub-<strong>xxxxxxxxxxxxxxxx</strong>"
     data-ad-slot="<strong>xxxxxxxxxx</strong>">
</ins>

xxxxxxxxxx - identifiers of the ad block tied to your account

And the part that actually renders the ad:

<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

Naturally the system suggests you dump all of this as one solid block on your site.

The section that loads the external adsbygoogle.js script is strongly recommended to move into the theme’s footer.php. Not a required step, but doing so means the javascript loads after the main content, which is a plus for the user appearance of your site.

You can edit footer.php right in the WordPress admin. From the Appearance menu pick Editor (image is clickable)
Screenshot from 2014-11-14 15:39:23

In the window that opens find the Footer file (footer.php).
Screenshot from 2014-11-14 16:22:36

Right before the closing </body> tag insert:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Back to the Appearance menu. This time we need the Widgets item
Screenshot from 2014-11-14 16:24:22

Pick the Text widget and drag it into place in the sidebar.
In the window that opens type the rest of the ad block code, as shown in the screenshot:
Screenshot from 2014-11-14 16:27:22

Hit Save and go check the site.

The ad won’t show up right away. Go make some tea while AdSense checks the code installation and generates the ad blocks.