You are here

How to Insert Adsense Into Content in Drupal 6

drupal logo

Drupal comes with many handy modules, not the least of which are the Adsense and Adsense Injector modules - which let you control the look, feel, and placement of your Adsense ads within your Drupal pages. The problem? The Adsense injector, while having the right intentions, isn't quite to the level where it will allow you to "inject" ads into the middle of your content. For the more technically inclined - in your theme's node.tpl.php file, the print $content statement prints the actual body of your content.

 

How to Insert Adsense Into Content in Drupal 6

 

METHOD 1:

For Drupal 6.x, the process is a bit simpler than in Drupal 5.x. First, you'll want to define a new region for your Adsense "in body" code in your theme's .info file. Beneath your defined regions, add a line that defines your new region (if there are no regions, make sure you redefine all your regions (left, right, header, etc.)

or they will disappear):

regions[left] = Left
regions[right] = Right
regions[header] = Header

regions[incontent] = In Content

Next, we expose this region variable to your node template file. Simply add the following to your theme'stemplate.tpl.php file:

function theme_name_preprocess_node(&$vars, $hook) {
// don't load region in teaser view
if ($hook == 'node' && !$vars['teaser']) {
$vars['
incontent'] = theme('blocks', 'incontent');
}
}

Make sure you replace theme_name with the folder (directory) name of your theme. The preprocess_node statement tells the function that we're addressing your node.tpl.php file.

Finally, the last step is to insert code in your node.tpl.php file that will actually insert the new ad region into the middle of your content:

<?php
$arr = explode ("</p>", $content); 
// center content ads
$arr[2] = $incontent . $arr[2]; 
$content = implode("</p>", $arr);
?>

What this does is simply insert the content of your incontent block after the second paragraph printed by your node.tpl.php file. So in effect, by assigning an Adsense block to your incontent block, your ads will be inserted after the second paragraph within your body content. To adjust the number of paragraphs used for insertion, simply adjust the 2 in the second line of the example above.

The only remaining step, to make this all work, is to actually assign an Adsense block to your new incontent region. Before you do this, you need to clear the theme cache, which will cause the changes you made to your .info file (the newly defined regions) to load. To do so, simply visit your theme select/ configuration page (via Administer -> Site Building -> Themes). If for some reason you get the message, when visiting your block page, that your blocks were disabled since they were assigned to invalid regions - don't panic, simply confirm that you have all your regions in place in your .info theme file, and then clear your theme cache as described above

Clear Your Cache

If your ads aren't showing up as intended, visit Administer -> Site Configuration -> Performance and clear your entire cache. Once you've done that, and the above code is in place, your ads should automatically be inserted in the middle of your Drupal content.

Styling The Placement Of Your In Content Ad Block

By default, your "In Content" Adsense block will cause all text to fall beneath it. The ad region will integrate even better with your content if the text falls around it, ie. to the right or left of it. To accomplish this, simply add the following line to your theme's style.css file:

/* adsense */
.content .adsense_managed { float: left; margin-right: 1em; margin-bottom: 1em }

In this example, your adsense block is floated to the left, so your content body's text will fall to the right of it, instead of beneath it. We also added in a little margin to give some space between the ad text and your body text, so the two don't appear to run together. Finally, if you wish to float the ad block to the left instead, simply change the float attribute to float: right, and adjust your margin to margin-left.

You'll notice that your Adsense block configuration allows a setting ad alignment, but all this does is align your text (ie. justify it via the CSS declaration text-align: left (or right, or center))- it will not cause your entire ad block to float to the left (or right) of your text.

 

 

METOHD 2:

 

For this, we need to add some code to the file 'node.tpl.php' which is located inside our drupal themes folder. Open this file on your text editor and look for the line where the variable '$content' is printed. To be more specific, open 'node.tpl.php' and look for the code section below, on the file:

<div class="content"><?php print $content?></div>

Now, we need to add a block of PHP code inside the div just before the PHP block displayed above. You can just copy and paste the code block below with no modification and it will just work fine for you.

 

PHP CODE BLOCK TO BE ADDED

<?php
// define a variable which holds a div to hold your adsense script
 
$incontent = "<div class='adsense_managed'>
		<!-- Copy and Paste your Google Adsense Code here -->
	      </div>";
 
// Check to prevent the ads display on the Front Page  	
 
if (drupal_is_front_page()) {
	// do nothing...the code will be unaltered
        // No google ads will be displayed on the front page Content body
}else{
	// Injects Adsense code after the 3rd Paragraph
 
	$arr = explode ("</p>", $content); // center content ads
	$arr[3] = $incontent . $arr[3];
	$content = implode("</p>", $arr);
}
?>

What we've done here is pretty simple. First, we define a variable 'incontent' that holds the div block with class 'adsense_managed'. Note the class name here, as it is required to tweak our CSS later. We then put our Adsense Code inside this div.

We then check to make sure the ads aren't displayed on the front page. This check has been done to prevent our front page from getting cluttered with the ads.

And now for the main part, the three lines of code then, inserts our variable containing the adsense code within the content body after the third paragraph. You can simply change the array key '3' in the example above as desired to display the ad after respective paragraph.

CSS TO STYLE THE PLACEMENT OF THE AD BLOCK
Our Ad block containing div needs some CSS styling to blend it with the content body. You can simply insert the CSS code section below to your CSS file 'style.css' which is also located inside the Drupal themes folder.

CSS CODE

/* styling the adsense code block */
.content .adsense_managed { float: left; margin-right: 5px; margin-bottom: 5px; }

The CSS code above floats the ad block to the left aligning the text content to its right. The margin are meant for maintaining a space between ad block and the content. You can set the float to 'right' and the ad block will be aligned to the right. If you do so, the 'margin-right' attribute should be changed to 'margin-left'.

NOTE:
This code has been tested to work correctly with the Drupal Version 6. I haven't checked it with other versions of Drupal.

 

source: werockyourweb.com, pujanpiya.com.np

 

How to Insert Adsense Into Content in Drupal 6