News Blog Communication: Google Adsense Shortcode

Info News


Google Adsense Shortcode



A friend asked me today was there an easy way to add Google adsense ads within WordPress without actually filling the template files full of the implementation code, the answer was yes.. use shortcodes!
So here is how you can add your Google adsense blocks to your blog without pasting lines and lines of code, the following snippets take place within the functions.php file, so remember to backup first.
Add to functions.php
function google_adsense() {
return '
'; } add_shortcode('adsense', 'google_adsense');
You will need to add your PUB ID, the ad Slot Number and the ad sizes (width and height), I also put it inside a class so you can style using CSS within your theme stylesheet like this:
.adsense {margin: 5px; padding: 5px;}
Now when you need to place an adsense block you can do so like this:
[adsense]
If you have a couple of different ad sizes you can reuse the snippet but rename the function and class then add different heights and widths like so:
function google_adsense2() {
return '
'; } add_shortcode('adsense2', 'google_adsense2');
and to call it in your pages and posts:
[adsense2]
 
Posted by Ybo