Adding Google Adsense Code Between Topics in phpBB

By | July 6, 2008

You can often see it even on popular forums. Google Adsense or any other ad code is inserted between posts or looks like a usual post. How do they do that?

I will show you how to add Adsense code after first post and I hope you will be able to figure out how to do it to put it at the middle of posts, as the last post, etc.

We will need to edit two files. They are viewtopic.php and viewtopic_body.tpl (located at the template directory). Let’s start with the first one.
In viewtopic.php in the main phpBB directory, at around line 826, you’ll find:

//
// Okay, let’s do the loop, yeah come on baby let’s do the loop
// and it goes like this …
//

Right under that is the statement that begins the looping through the posts:

for($i = 0; $i < $total_posts; $i++)
{

Under this statement let’s add the block that shows ad code:

if ($i==”0″) {
$adblock = $adblocktext;
} else {
$adblock = “”;
}

Next we should define $adblocktext – it is the variable containing ad code.

$adblocktext = <<<EOM
<tr><td colspan=2 align=center>
Insert your Google AdSense Code Here
</td></tr>
<tr><td class=”spaceRow” colspan=”2″ height=”1″><img
src=”templates/subSilver/images/spacer.gif”
alt=”” width=”1″ height=”1″ /></td>
</tr>
EOM;

Now, let’s declare our ad variable. Scroll down to around line 1170 to find something like this:

$template->assign_block_vars(‘postrow’, array(
‘ROW_COLOR’ => ‘#’ . $row_color,
‘ROW_CLASS’ => $row_class,

In that block we need to add the following line:

‘AD_BLOCK’ => $adblock,

That’s all with viewtopic.php. Variables are defined and assigned. Now we need to modify our template to get this working.

Let’s open viewtopic_body.tpl. It should be located in the templates directory under the template that is used at your forum.

Let’s find the line with the following text:

<!– END postrow –>

Right above it, we will add the following text:

{postrow.AD_BLOCK}

That’s all! Don’t forget to save both edited files and have fun! Your code will be shown after the first post. You are able to use JavaScript, Google Adsense code or just everything you want.

One thought on “Adding Google Adsense Code Between Topics in phpBB

  1. Pingback: Adding Google Adsense Code Between Topics in phpBB

Comments are closed.