Insert Adsense To WordPress Posts Using Shortcodes

I see a lot of people who every time they want to add some adsense to their posts they need to login to their google account, find the adsense they want, copy it, then go all the way back and paste into their post (in HTML view).

Now wouldn’t it be better to have all the adsense you will ever use on your site already, so all you need to do is add a quick shortcode when you make a post to “magically” bring out the adsense you want? All in Visual mode, no need for HTML mode and no need for plugins.

Well the former is actually very easy. With the following method, you will never have to cut and paste your adsense into your posts. Instead all you will need to do is write something like… [adsense].

Creating a shortcode

Before I really knew anything about WordPress, shortcodes looked so hard, that I didn’t want to mess with them. However, they are actually REALLY easy!

There are 4 main steps of creating and showing your shortcode:

  1. Define the start a function
  2. Define a return value for the function
  3. Create the shortcode
  4. Add the shortcode to your posts

Define the shortcode

This may seem obvious, but what I mean by defining the function, is that first we need to tell WordPress that we are creating a function and we need to give this function a name.

So you would start out with something like this:

function showads() {

Note: The term “showads” is simply what I named the function. You can call it whatever you want. If you are going to make a shortcode for all your ads, you may want to name each one so you know what it is.For example: “showads_image_250×250″, may be a good name for your 250×250 pixel image adsese.

Define the functions return value

After we have named our function (show ads) we now want to define what value the function should return when executed.

We do so like this:

return ‘<script type=”text/javascript”><!–
google_ad_client = “pub-6577026543227412″;
/* WpExplorer */
google_ad_slot = “1449553805″;
google_ad_width = 300;
google_ad_height = 250;
//–>
</script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>’;
}

Create the Shortcode

We have given the function a name and told it what we want in return. Now we just need to create the shortcode.

The shortcode is created like this:

add_shortcode(‘adsense’, ’showads’);

Add the Adsense Shortcode to your Posts

Now to add the adsense shortcode to your posts all you need to do is add the following wherever you want the adsense to appear.

[showads]

Complete Adsense Shortcode

The Complete Adsense shortcode if you want to copy and paste it looks like this:

function showads() {
return ‘<script type=”text/javascript”><!–
google_ad_client = “pub-6577026543227412″;
/* WpExplorer */
google_ad_slot = “1449553805″;
google_ad_width = 300;
google_ad_height = 250;
//–>
</script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>’;
}

add_shortcode(‘adsense’, ’showads’);


Leave A Comment