Top Free Wordpress Themes Rated by Users, Live Demo and Free Download.

WordPress Adsense Shortcode – Randomize your ads position

How to Insert Adsense To WordPress Posts Using Shortcodes?
Short answer: create the shortcode and add to your functions.php, then add the shortcode to your post.

wordpress-adsense-shortcode-code

Why you need shortcode to add adsense in your wordpress post?
You can easily add Adsense ads in your posts by inserting a very simple code. By using wordpress shortcode the whole process is incredibly easy, and saving your time. WordPress Adsense shortcode also help you to randomize your ads positions.

See the result below
adsense-shortcode-for-wordpress-result

Now, here the code. I found this adsense shortcode at blue-anvil, and modified it for my own use.

function adsense_shortcode( $atts ) { extract(shortcode_atts(array(
	    'format' => 'none',), $atts));
		switch ($format) {
		case none :
			$ad = '<script type="text/javascript"><!--
google_ad_client = "pub-00000000000000";
/* 336x280, created 3/15/11 */
google_ad_slot = "00000000000000";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
		break;
		case right : $ad = '<div class="alignright"><script type="text/javascript"><!--
google_ad_client = "pub-00000000000000";
/* 336x280, created 3/15/11 */
google_ad_slot = "00000000000000";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>';
		break;
	}
	return $ad;
}
add_shortcode('ads', 'adsense_shortcode');

Add that code to your theme functions.php. Once you created your function, you have to use the shortcode function in your post.

Add this style to your style.css

.alignright {float:right;}

USAGE :

[ ads ] : use this shortcode without space in your post. This will show your ads without alignment.
[ ads format="right" ] : this shortcode will float on the right of your content. Discard space before open bracket “[" and close bracket "]”

The wordpress documentation for the shortcode API can be found here http://codex.wordpress.org/Shortcode_API

Incoming search terms:

  • free wp adsense shortcodes
  • Shortcode ADS
  • wordpress position code

One Response to “WordPress Adsense Shortcode – Randomize your ads position”

  1. [...] WordPress Write Panel. First, we can easily added custom content to our wordpress post. If you use Shortcode to Randomize adsense position, this code will save your [...]

Leave a Reply

About img

This website is a personal wordpress theme documentation. However, rather than stored in the computer and become obsolete, I decided to create online documentation. Free WordPress Themes available for download on this website was a private theme that I use for my blog. Files and functions on these themes were taken and mix from other [...]