As the title goes, this tutorial will pretty much try to cover (at a beginner level) all the steps that need to be taken in order to have the power of choosing in which articles and where exactly to post your ad blocks. An ad block can be AdSense, AdBrite, basically it doesn’t matter if it’s a link to an image or a JavaScript piece of code. The advantage of doing this is that you can always prepare something new for your readers. You won’t always get to have that 468×60 under your post because having the same positions for the ads will make them invisible for your visitors. So I will show you how to get things in control and put those ads where you want, and when you want them.
The first thing you’ll need to do is to open your functions.php file of your theme with a basic text editor (Notepad will do). If you don’t have one, just create the file now (it should be located in your theme’s folder). Add the following piece of code to the end of the file (in this tutorial I will be editing the functions.php file of the default theme):
<!– here starts our custom code –>
function adsense_shortcode( $atts ) {
extract(shortcode_atts(array(
‘format’ => ‘1’,
), $atts));
switch ($format) {
case 1 :
$ad = ‘<script type=”text/javascript”><!–
google_ad_client = “pub-xxxxxxxxxxxxxxx”;
google_ad_slot = “xxxxxxxxx”;
google_ad_width = 468;
google_ad_height = 60;
//–>
</script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>';
break;
}
return $ad;
}
add_shortcode(‘adsense’, ‘adsense_shortcode’);
<!– the custom code just ended here –>
All you need to do next to output the ad block in your post is to use [adsense] syntax. Notice the $ad variable. In this example it holds the basic AdSense code. Feel free to replace that code with any ad code. As I said earlier, you can have something as basic as a simple image that links to some site (with referral code or such). It is important to keep in mind the size of the code that you’re using. You probably don’t want to end up putting a wide skyscraper in your article; but then again… who knows. If you want to have different ads just duplicate the code above and instead of the following lines have adsense replaced with the new name:
function adsense_shortcode( $atts ) {
replace with
function newName_shortcode( $atts ) {
and
add_shortcode(‘adsense’, ‘adsense_shortcode’);
replace with
add_shortcode(‘newName’, ‘newName_shortcode’);
That should take care of having multiple ad variants. Don’t forget that now you need to call the new ad block by using [newName] instead of [adsense]. Of course there are other more complex methods of doing this but the fact is that I wanted to write a very very easy to follow tutorial.
Hope this was useful for at least someone out there. If you don’t want to dirty your hands with code then you should probably use the WhyDoWork AdSense Plugin.