Due to the popularity of our web development contract template article, we're offering the contract, estimate, and invoice templates for $199.50 $49.50 if you order by March 14. Click here to learn more.
Constant Contact has been in business since 1996 is one of the most reliable and trusted email marketing services around. Click here to activate your free 60 day trial courtesy of CWD Community.
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.
Ideally, the Adsense Injector module would allow you to inject Adsense ads into the middle of your body content (within the content printed by $content). However, at this point it only allows injection before and after $content, in other words, before and after your body content. The solution is to tweak your template files so you can inject an ad block into the middle of your Drupal content following a specified number of paragraphs (you determine this number and can change it to your liking).
We're going to show you how to insert your ads right into the middle of your content - not at the beginning or the end. The implementation of this tweak is going to depend on the version of Drupal you're using. In this article, we're going to cover Drupal 5.x and Drupal 6.x. If you're using another version, simply post below and we'll do our best to accommodate your request.
If you're using Drupal 5.x, you'll need to edit the following files to have Adsense inserted after 2 paragraphs of your body content (simply change the number 2 if you wish to adjust the number of paragraphs following which ads are inserted).
First, we'll add a block region to your template, which will control the placement of your "in content" ads. To do so, simply modify the template.tpl.php file within your theme directory, adding the following:
/* in body ad placement */
function template_name_regions() {
return array(
'right' => t('right sidebar'),
'left' => t('left sidebar'),
'content' => t('content'),
'header' => t('header'),
'footer' => t('footer'),
'incontent' => t('in content')
);
}
function _phptemplate_variables($hook, $vars) {
// region distributor
$variables = array();
// don't load region in teaser view
if ($hook == 'node' && !$vars['teaser']) {
// load region content via block assignment
foreach (array('incontent') as $region) {
$variables[$region] = theme('blocks', $region);
}
}
return $variables;
return $vars;
}
You'll notice that we added an 'incontent' region. When you administer your blocks, you'll be able to select this new region from the block placement drop-down menu. It's important that you replace template_name in the above example with the folder name (directory name) of your theme.
Next, you'll want to add the code that actually inserts your Adsense ads into this newly created block. Add the following at the top of your node.tpl.php file:
<?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. 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.
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's template.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
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.
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.
All Content © 2005 - 2010 Contract Web Development, Inc. All Rights Reserved. Privacy Policy | Terms of Use | Powered by Drupal
no template.tpl.php in bluemarine theme
There is no template.tpl.php in the bluemarine theme for drupal 5.x.... where should I place the code?
Very cool. 2 questions...
What would it take to define the location based from the bottom of the node up. That is, the number of paragraphs from the end, rather than the beginning?
Also, how would you achieve an inline ad at the end of each story, so that it is flush with the bottom of the body text, but before links, taxonomy, etc?
Many thanks!
Insert ads from bottom
Good question, not sure there's an easy way to count from the bottom. As for an inline ad at the end of each story - if you place the ad in a block, and place the block in the "content" area - it should be placed at the end of your content, but before your links, by default.
Perfect!
Very clever
helped a lot! Thanks!!!
Great!
wow...thx so much... i was educated..hehe i'll be telling this to my friends... regards
great tutorial
This works great with the basic theme - which is probably the baddest theme out there to customize.
Ad not showing up
First of all, awesome script, thank you.
I'm using the ThemeKey module to switch between two themes on my site based on taxonomy (vid), and the Adsense block shows up in one theme, but not in the other. I've compared theme functions and files to ensure they all match up, and literally everything is identical between the two themes, with exception of their name of course, and the fact that one has a different logo.
Any ideas why on earth the ad block is not showing up in one of the two themes? I've cleared caches, run cron, turned off caching, all to no avail.
Duplicate theme .info files?
Make sure your theme directory only has one .info file in it - the one corresponding to your theme name. If you inadvertently left another one in there, such as the one from the first theme, it will screw things up.
Thanx
Thank you very much for this great code. I used only one part of it.
Great example
I reused an existing Adsense block (adsense_content), so I only had to add this in the node.tpl.php:
$arr = explode ("", $node->content['body']['#value']); // center content ads
if (count($arr) > 5) {
$arr[1] = $adsense_content . $arr[1];
$node->content['body']['#value'] = implode("", $arr);
}
I print the recycled Adsense block at the bottom of all pages, and only inject the block if there are more than 5 on the page.