Easy Adsense inclusion with WordPress shortcode.

Your new blog has been built. You have decided to run a WordPress blog and already have a lot of ideas and themes to talk about. Another thing is that you might want to make some money off your blog just for the fun of doing it. Chances are that you will be running Google Adsense on your blog. Why is that so? Just because Google is well known, adsense integration is fairly easy and “everybody else is doing it”.

The semi-problem.

Setting up Adsense on your blog is not a problem in itself. Once your theme is ready you just have to head over to the Adsense portal and create your dedicated ad group and copy and paste the code in your template (if automatised) or in a text editor to copy and paste in each of your posts. But did you know that there was an easier way of including your ad in your text? It is called WordPress shortcode.

WordPress Shortcode.

Basically, WordPress shortcode allows you to generate a specific piece of code in any part of your post without having to write the whole code. Those who are code learned will understand if I say that it is the equivalent of a PHP include or a Java import. Those who are not will understand if I say that it helps you write the equivalent of 10 lines while you’re writing only one word. This is an integrated behaviour of the WordPress core. So you don’t have to activate anything. You only have to have access to your template functions.php file. The easiest way to do this is to use your theme editor in WordPress: Appearance > Editor.

Adsense shortcode.

Once you have generated your Adsense code you just have to copy and paste it like this (this is my ad code, you need to replace the code starting with script):

function showads() {
    return '
		<div class="publicity">
         	<script type="text/javascript"><!--
			google_ad_client = "pub-7734054350163682";
			/* 468x60, date de création 05/08/09 */
			google_ad_slot = "4764339399";
			google_ad_width = 468;
			google_ad_height = 60;
			//-->
			</script>
			<script type="text/javascript"
			src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script>
		</div>
';
}

Once this is done, just add this code under the function :

add_shortcode('adsense', 'showads');

Using it in your posts.

Now you’re done. Anytime you’re writing a post now, you can invoke your ad banner by just typing the shortcode between squared brackets like this:

[adsense]

This saves you the trouble of copying and pasting your adsense code everytime you want to make use of it.


About the Author:
Sachin D. Brojmohun has extensive experience in terms of graphic design, CSS integration, usability and accessibility as well as in SEO. More about him and the Web Design Bureau of Mauritius here: Web Design Mauritius.

Comments (5)

Trackback URL | Comments RSS Feed

  1. Kurt Avish
    Twitter:
    says:

    Interesting. Didn’t know of the function method. I normally place the code in the theme code itself.

  2. Tushal
    Twitter:
    says:

    Thank you.. :idea:

  3. Rikesh says:

    Great idea to retweet this post :)
    I didn’t know about this method.
    Gonna implement it on the spot!