This is my first ever blog post, so I'm going to keep it simple, and start with this easy trick to add adsense to your articles. This trick does not require the usage of the adsense module, or any other module. Actually, this trick does not even require adsense, because in essence, we'll be creating an in-content block in wich you can put any type of advertising you like.

First of all, we're going to create a new block region. Put the following code in your template.php file, if you don't have one, create a template.php file and put it in the directory of your theme:

function yourtheme_regions() {
return array(
'right' => t('right sidebar'),
'left' => t('left sidebar'),		
'content' => t('content'),
'header' => t('header'),
'footer' => t('footer'),
'incontent' => t('in content')

Make sure that you replace yourtheme with the actual name of your template, as shown in your theme administration page. Note that if you're creating a template.php file, you need to engage PHP in your file by putting <?php at the first line above our function and ?> right below our function.

Now that's done, we're going to create another function in template.php which will allow the block region to be loaded in a node template, because by default, a block region can only only be loaded in a page template.

function _phptemplate_variables($hook, $vars) {
//region distributor
$variables = array();
// Load the node region only if we're not in a teaser view.
if ($hook == 'node' && !$vars['teaser']) {
// Load region content assigned via blocks.
foreach (array('incontent') as $region) {
$variables[$region] = theme('blocks', $region);
return $variables;
return $vars;

This code doesn't need any editing, just paste it into your template.php file and you're done. If you already have a phptemplate_variables function in your template.php file you need to take a good look at the brackets and mix this code with the existing function. Now for our third and last step, we're going to open our node.tpl.php file, also known as the node template. If you can't find a node.tpl.php file in your theme directory, copy the one from the default theme bluemarine.

Now that you've opened your node.tpl.php, paste the following code at the very beginning of the file, leaving all of its initial content below it:

$arr = explode ("</p>", $content); 
if ( strpos ($arr[0], "</span>") > 0 ) { 
$arr[1] = $incontent . $arr[1]; 
} else { 
$arr[0] = $incontent . $arr[0]; 
$content = implode("</p>", $arr);

Basically what this code does, is exploding your content into little pieces, using a paragraph as definition for one piece, then it will look for paragraphs inside your contents. By default, it will place the adsense code just after the first paragraph, at $arr[0], but when it finds a </span> tag in the first paragraph, the script will move the incontent block down to the next paragraph. Why is this, you're probably wondering...

When I first used this code, I found out that on some pages where I had added an image at the beginning of the article, the adsense block and the image were packed together, ruining my layout! Therefore, I'm using the end span tag as an indication of an inline image being present; because when adding an image to your article with img_assist, the image will be wrapped in a <span class="inline left/right"> element, and by skipping the first paragraph with the image your layout will be preserved..

To finish up, all you need to do is add a block, put adsense code in it, at place it in your new incontent blockregion, and marvel and your newly gained in-content ad-block. Just a hint, if you want the surrounding text to wrap around your adsense block, you have to float it, using css. One way to float it is by wrapping your adsense code in a floating div element:

<div style="float: right">
//your adsense code here

In retrospect, I guess this trick isn't really that simple, I'll try harder next time. Allthough later on I also want to continue in this topic with some more advanced adsense tricks.


Nov 17, 2007 By Jurriaan