Blog Post

WordPress and Google AdSense

NOTE: The following has only been tested on version 3.1 of WordPress. It is not my fault if stuff blows up, so follow at your own risk.

Since I have started using Google AdSense on my blog, I have always used the same size ad on every page. The reason I did this is because of space allotment, and I didn’t know any better at the time. The other day while I was researching AdSense, I was reading about targeting and the different channels. After reading, I created a few different sized ads and tried to figure out how to fit them in to my website. I knew on pages like my blog, where the page was long, I could use a skyscraper ad, but on pages that were smaller, such as as my Contact or Donate pages, I couldn’t use such a large ad. Instead of mucking up my page with Javascript, or better yet my sidebar widgets since that is where my ads are placed, I decided I wanted to go the PHP route, do a single call to the database to get the page name, and go from there. If you have any experience with WordPress, one thing you will notice is you can’t include PHP markup in the body of any pages, posts, or widgets. This is a good thing, as it can potentially become a security risk. Sure there are different plugins you can use that will allow you to use PHP in pages or posts, but I decided on one.

I chose to use Shortcode Exec PHP (SEP). I read reviews, checked out the forums, making sure nobody was having major issues or security issues. What SEP does is allow me to create PHP snippets in my WordPress Settings, and call them with short code in a page, post, or widgets. For instance, for my ads I would add [adsense] where ever I wanted to place my customized ad format.

To do this, you first need to install SEP into your WordPress site, activate it, and then get to writing your snippets. To access the page to add, delete, or edit snippets, go to Settings → Shortcode Exec PHP. On this page you will see a box or boxes for your snippets.

NAME represents the name of the short code that you will call within the brackets in a page, post, or widget. SHORTCODE SNIPPET is the actual PHP markup you will use for this snippet.

So in Google AdSense I have gone ahead and created 3 different ads, each have a different size.

  • Blog page and other longer length pages have a text/image skyscraper ad that is 160×600 in size and is in the sidebar.
  • Contact page has a text/image box that is 250×250 in size and is in the sidebar.
  • Donate page is short, so neither of the 2 above will fit it correctly, so I created a text/image ad that is 468×60, and isn’t in the sidebar.

So, since I am targeting the sidebar widget here, I am just going to look at the ads for my blog and contact pages to do some shortcode for. Like I stated earlier, I gave this shortcode the name adsense. In the shortcode snippet box I added the following PHP code:

global $post;
if ($post->post_name != 'donate') {
    echo '<script type="text/javascript"><!--';
    echo 'google_ad_client = "ca-pub-0123456789876543210";';
    if ($post->post_name != 'contact') {
        echo '/* YourAdsUniqueName */';
        echo 'google_ad_slot = "0123456789";';
        echo 'google_ad_width = 160;';
        echo 'google_ad_height = 600;';
    } else {
        echo '/* YourAdsUniqueName */';
        echo 'google_ad_slot = "0123456789";';
        echo 'google_ad_width = 250;';
        echo 'google_ad_height = 250;';
    echo '//-->';
    echo '</script>';
    echo '<script type="text/javascript" src="">';
    echo '</script>';

NOTE: your YourAdsUniqueName and google_ad_slot information will be different.

After adding this, I click the Add button, which adds the shortcode and now allows you to edit it, test it, or delete it. In this case testing it won’t be of much good, as it will only return the portion of the add in the else portion of the if statement. Next you will want to make sure that both the Enabled and Output echoed boxes are checked.

Next go into your widgets and add a Text widget. In the large post box, all you need to add is [adsense]. You can wrap this in a div if you need to do any formatting.

That’s it, now I will have a different ad whether it is on my donate page, a blog page, or a page that is longer.

This entry was posted in Development and tagged , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • Archives