Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

phpBB trick for including Google AdSense between articles?

I'm running phpBB for my site discussion forums and really like it, but I'd like to slip some advertising between forum posts #1 and #2 on my pages. For the life of me, though, I just can't figure it out. Can you help out?


Dave's Answer:

Luckily, I bumped into a fabulously detailed answer to this very question on a private mailing list, and the author, Greg Bulmash, has agreed to have it republished here at Ask Dave Taylor:

EDIT VIEWTOPIC.PHP

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 that I added...

if ($i=="0") {
$adblock = $adblocktext;
} else {
$adblock = "";
}

Up near the beginning of the file, I defined the new variable $adblocktext thusly:

$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;

The table structure here is important. The first block puts in your Google AdSense code. The second block is a separator line.

Now, scroll down to the line 1170'ish range of the file and you'll find a block of variable declarations that begins with:

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

In that block, on its own line, add:

'AD_BLOCK' => $adblock,

Save that file.

EDIT VIEWTOPIC_BODY.TPL

Now go into the templates directory, and into the subdirectory of the template you are using (i.e. subSilver). Open the file viewtopic_body.tpl for editing.

Down around line 61, you'll find a line that says:

<!-- END postrow -->

Right above it, add the following text...

{postrow.AD_BLOCK}

Save the file, and you're done!

Now your forums will insert whatever you wish after the first post. And yes, you can insert Javascript. I tried the following...

$adblocktext = <<<EOM
<tr><td colspan=2 align=center>
Insert Google Code Here  <P>
<script language=javascript>
document.write("hoobastank");
</script>
</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;

and it worked just fine.

Contributor Greg Bulmash has lots of cool Web sites, but invites readers to pop over to his fun baby clothes site. He says he could use the sales. :-)


Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Hi,

Thank you very much for this advice!

It took me about 45 minutes to figure out why it wasn´t working... I pasted the "{postrow.AD_BLOCK}" *below* ""...

Thats because my English is very poor and I frequently misunderstand "below" and "above"...

Thanks again,
Tiago Serafim

Posted by: Tiago Serafim at July 12, 2005 2:36 PM

Hello,

Thanks for that. How can I make it so that a 728x90 ad will be centered on a 800x600 resolution? Right now it causes horizonal scroll. Any ideas would be great.

Thanks.

Posted by: Patrick at August 16, 2005 11:18 PM

Just tried this on my new forum - works perfectly, and I don't know anything about web code! Thanks so much.

Posted by: Sophie at November 29, 2005 3:16 PM

thankyou very much... it worked without any hard work..... thanks

Posted by: Abdullah at January 27, 2006 3:41 PM

Any demo?

Posted by: nicepuppy at February 6, 2006 7:35 PM

thank you for your advise, this code works perfectly!

Posted by: China Guide at February 7, 2006 4:20 PM

this works so brilliant! I got some other code before, but this one work without any harssle! thanks mate!

Posted by: Oneandhalf Web Server at February 11, 2006 10:59 AM

thanks man i wanted to find that for my site onlineludhiana.com

Posted by: sarvpriya at March 15, 2006 9:05 AM

I have tried it several times and it does not work, any post i do now comes out blank, i am using phpbb version 2.0.19, please advise as to what i may be doing wrong

Posted by: ravs at April 11, 2006 8:33 AM

Excellent, I can confirm that it works great on 2.0.20, If anyone is having problems, double check your code changes.

www.studio7wd.com

Posted by: Studio 7 at April 24, 2006 2:51 AM

Thanks. I use this feature in my forum and waiting for good money.

Posted by: aurumgames at May 2, 2006 2:05 PM

Thank you for this excellent sample code. We have been searching for something like this for some time now.

Posted by: MoJeepin.com at May 17, 2006 1:29 PM

This is great! It took me just a few minutes to install it on my phpBB forum at www.HURT911.org and at www.Lawyer-Advertising-Blog.com. It worked perfectly the second time (the first time was my fault). One note: I didn't take out "Insert your Google AdSense Code Here" in viewtopic.php because I added // in front to make it a comment. That didn't make it a comment, so I deleted it and it worked fine.

Posted by: Philip Franckel at June 24, 2006 10:11 PM

I'm sorry but it didn't work. I've checked the code a couple of times with no luck. Any ideas ?

Posted by: MD at July 10, 2006 4:56 PM

Up near the beginning of the file, I defined the new variable $adblocktext thusly:

$adblocktext =
Insert your Google AdSense Code Here


EOM;

where should i put this part.....

Posted by: vj at July 16, 2006 5:22 AM

Great mod! I installed and am using it in
conjunction with phpadnews and it works
seamlessly. check it out if you want
www.rateyourbiz.net/forum

thank you very much

Ryan

Posted by: Ryan at July 17, 2006 1:48 PM

Where exactly do I put this? I mean what line range?

"Up near the beginning of the file, I defined the new..."

Posted by: Tom Tomyt at July 19, 2006 8:09 PM

Nice shortcut!

Posted by: Andrew at July 23, 2006 3:54 AM

I did it! And this is the first time I hack into my phpBB code. Phew...Thank you!

Posted by: lilian at July 25, 2006 10:55 AM

if ($i=="0") {
$adblock = $adblocktext;
} else {
$adblock = "";
}Up near the beginning of the file, I defined the new variable $adblocktext thusly:

$adblocktext =
Insert your Google AdSense Code Here

EOM;


--------------------------------------------
do it like this......

---------------------------------------
if ($i=="0") {
$adblock = $adblocktext =

EOM;
--------------------------------

works like a charm i cnt belive i got this code n got it workin after intense searchin of 2 months finally its here workin like a charm on phpbb 2.0.20

thanx alott u r the mannn!!

Posted by: rafay at August 6, 2006 9:35 PM

Not working for me. Tried couple of times with different phpbb forums. Any ideas ?

Posted by: Dat at August 27, 2006 1:03 PM

Works well. But in 800X600 resolution, I get horizontal bar because of the code.

Posted by: Anand at August 29, 2006 9:18 AM

Thanks a lot for this. It was very easy to make. Thanks.

Posted by: Siyah at August 29, 2006 7:35 PM

Wow! It worked perfectly for me. Thanks to the author and DAVE as well for bringing up this.

Posted by: Saurabh at September 16, 2006 4:07 AM

nice modification man. thanks for the help.

Posted by: ludhiana at September 16, 2006 12:52 PM

hi.. i have truble with the code

$adblocktext =
Insert your Google AdSense Code Here

EOM;

i have to delete EOM? what is that? i delete the code but i get

Parse error: parse error, unexpected '=' in /viewtopic.php on line 3

!!??? help pleaseª!

Posted by: locoman at September 27, 2006 8:33 AM

Hey thanks for the code its works great. Currently tested it on the OR Forum @ www.brainhead.info. The site though is still under construction though. But thanks again. Really appreciate it.

Posted by: OR Forum at September 27, 2006 1:34 PM

hi,
can someone tell me how i can change the background color of the field where is the google adsense in?

now its white and i will it have blue.

can someone help me?

--------------------------------

hi,
kann mir einer sagen wie ich die hintergrundfarbe änder?
im moment ist sie weiß ich würde sie aber gerne blau haben.

weiß das einer?

danke

thx

cu vip

Posted by: Viperdriver2000 at October 12, 2006 2:48 PM

Ok well I didn't like how it looked so after a little tweaking of my own I got it to look like an actual post just minus a few things. So if you do want it to look like an actual post then follow the tutorial exactly but just use this code instead: http://img178.imageshack.us/img178/3631/codeoi3.png

I put the code onto an image so you could see the entire code since it's a bit longer but I like it much more now cause it blends in with the forum and looks much better.

Posted by: eyeinthesky43 at December 4, 2006 9:29 PM

working fine...thanks :)

we can also change if ($i=="0") to if ($i < $total_posts) to make the ad appear after every post.

Posted by: Lovedeep Wadhwa at December 30, 2006 3:56 AM

Wow, thanks for making it so simple.

@Viperdriver2000: the background color of the Google ads are done in the actual adsense makeup on the Google page.

Posted by: Brian at December 30, 2006 4:30 PM

First Time, Perfect.

Great Tutorial Dave, Thanks a Million

Posted by: Keith Donegan at January 1, 2007 11:09 PM

Great tutorial Dave, it worked the first time. Thank you!

Posted by: Gentry Sherrill at January 24, 2007 5:29 PM

Does it take 48 hours for ads to appear?

Posted by: b at February 15, 2007 1:59 AM

Does this work for phpbb in phpnuke?

Posted by: Abe at February 19, 2007 3:20 PM

Just to update, I had to play around with this.

Posted by: Abe at February 19, 2007 3:55 PM

Great little MOD. I'm curious though if I could, instead of creating white space behind the ad, make the background color (#d5cba1) be what is seen behind the ad. Possible?

Posted by: Jeremy at February 27, 2007 9:58 AM

Jeremy, sure, there's no reason you can't go into the AdSense code block itself and set a specific background color, You can do that on the AdSense site itself, or you can just dig into the JavaScript block and set the value google_color_bg to whatever you want. On this page, for example, I use "google_color_bg = "ffffff";" to get the white background.

Posted by: Dave Taylor at February 27, 2007 10:42 AM

Thanks for the response but I'm not actually talking about the background of the google ad itself. The ad doesn't take up the entire space between the first and second post. Around the ad it's just white and I'd like to be the other color I posted before. Is that possible?

Posted by: Jeremy at February 27, 2007 1:55 PM

I don't know the internals of PHPbb but generally you can wrap the AdSense block in a div tag and use something like:

<div style="background-color:#ccf;width:100%">
adsense code block from Google
</div>

and that should hopefully accomplish what you seek!

Posted by: Dave Taylor at February 27, 2007 2:31 PM

If you are using PHP Nuke 7.9 and the integrated forum, the viewtopic_body.tpl file to edit should be the one in root of your site, in \themes\DeepBlue\forums (if you don't then your site will appear to be doing nothing at all). This only applies to PHP Nuke installs that use the forum module pre-configured. You would also need to adjust the code for each template used. I had to do this to http://www.elitebattlemasters.com I hope it helps!!

Fish-Guy

Posted by: Fish-Guy at February 28, 2007 9:14 AM

Thanks for this code - it works perfectly on my site at www.canadahealthinfo.ca/forum.

Posted by: Jack at February 28, 2007 10:22 PM

very nice! Once I opened the files it took about 5 minutes to change them and then done. Great step by step guide.

Posted by: Jacob at March 14, 2007 11:16 PM

it worked for me too ! very nice !

Posted by: Emil at March 20, 2007 12:03 AM

Doesnt work for me. Can anyone pls help me with details.

Posted by: abhi at March 24, 2007 11:30 PM

I am using MorpheusXDarkBlue as my style. I couldn't find the end post row line in the file.
Has anybody figured out how to do it using this style?

Posted by: sherwin at April 8, 2007 2:04 PM

is there a way to make ads appears after certain number of post.or before latest post, most people view latest posts not first ones?

Posted by: david at April 8, 2007 9:09 PM

Thanks for this code.

And yes, you can insert Javascript. I tried the following...$adblocktext = <<<EOM
<tr><td colspan=2 align=center>
Insert Google Code Here <P>
<script language=javascript>
document.write("hoobastank");
</script>
</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;

How to use javascript i dont understand.I am begener.

Posted by: Amko at April 13, 2007 3:39 AM

Okay, i tried it, and then it said:

Parse error: parse error, unexpected ';' in /sdc/home/shruikan/public_html/viewtopic.php on line 1412

so i undid it. and it's still doing it! look: http://www.shruikanforums.com/viewtopic.php

Posted by: Joseph at April 13, 2007 9:11 AM

What do you mean by "Up near the beginning of the file, I defined the new variable $adblocktext thusly:"

Where the hell is that!!!

Posted by: Cathal at May 2, 2007 3:27 AM

Many Thanks!

This has just saved me a lot of head scratching on a current project!

Chris

Posted by: Chris at May 21, 2007 1:14 PM

good work on this article, but I have a different question, how would I do this on phpbb3?

Posted by: Erick at May 30, 2007 1:48 PM

Cool,
How do I change if I want it to come after for example third post?

Posted by: Kory at June 5, 2007 3:56 AM

it worked for me too ! Thanks! !

Posted by: Mike at June 12, 2007 3:23 PM

hey thank you very much for that Ive been looking all over for that

Posted by: jessica at July 5, 2007 1:14 PM

Thanks good advice, it works !

Dan

Posted by: Dan Horton at July 9, 2007 5:49 AM

Thaanks...Great Help ...You made my work easier

Regards

VK

Posted by: vk at August 16, 2007 11:23 AM

hi Is this script a wind up?? Can anyone show me a forum where it works as all the ones mentioned above dont have it working!!!!

Posted by: toolpusher at August 27, 2007 9:33 PM

OOPS sorry found it working on some sites but spent two days on mine just about giving up!!!

Posted by: Toolpusher at August 27, 2007 10:18 PM

Yah ask Dave taylor, but where is dave taylor when many people ask this sentence ?

----------------------------
Where exactly do I put this? I mean what line range?

"Up near the beginning of the file, I defined the new..."
------------------------------

may be the owner must change "ask Dave Taylor" to "don't ask dave taylor"

Posted by: sys at August 28, 2007 3:20 AM

Easy there, sys. With over 100 comments a day added to the site, I can't field every question from every user. Further, I admit, I am pretty clueless about PHP and it was a friend of mine who helped out with this one. If you want to be helpful, please come back once you've figured this out and post your solution for others to read and learn from. That'd be a community service. :-)

Posted by: Dave Taylor at August 28, 2007 7:20 AM

thanks for your information, it really helped me in instaling as i liked google

Posted by: mark at September 14, 2007 7:03 PM

This worked brilliantly!

Thank you!

Posted by: George at September 21, 2007 7:08 AM

Great tip worked a charm www.scripts2go.co.uk

Posted by: David at October 9, 2007 6:57 AM

GREAT GREAT GREAT! It works perfect on last 2.0.22 version! It must works! Can`t believe that was so simple.
1st time didn`t works, my fault! You must be careful wih


Right above it, add the following text...
{postrow.AD_BLOCK}

open this tpl file with wordpad and do this

{postrow.AD_BLOCK}


bye
www.tribe.hr
www.ckscena.com
CROATIA

Posted by: Antac at October 13, 2007 2:08 AM

Worked like a charm! You are the best. Thank you

Posted by: Tim at November 16, 2007 12:03 PM

how to integrate adsense with andromeda(mp3 server)

Posted by: jeswinder singh at December 5, 2007 2:01 AM

Very Nice!!! This code work perfect in 2.0.22 !!!
Thanks, sorry for my poor english hehehe ;)

Posted by: Caballerus at December 6, 2007 7:07 PM

Works great, only one problem it stretches the whole of the 2nd column to the same width as the ad in IE only?
is it not possible to align in centrally without altering the width of the 2nd column?

Posted by: wd_2k6 at December 15, 2007 5:00 PM

I think that my above problem might be due to a bug in IE i hear, is there any other workaround other than using colspan? i did try nesting tables but it just messes up my forum, can anyone help.

Posted by: wd_2k6 at December 15, 2007 6:41 PM

hey guys ,this is so amazing and simply mode :))

thanks a lot ...but I did mistake in a first part , because of the term"up to next"
first time I added it under that,just up to...but it doesnt work ,so I move it above and its working great...

such as use here insert your adsense code

Posted by: cviky na břicho at February 25, 2008 9:42 AM

Couldn't find the right place he is talking about. I also have phpBB3 im kind of lost here.:-(

Posted by: Pramod at March 6, 2008 4:22 PM

Thanks so much, finally i could find a mod that worked on my phpbb.

Posted by: medical at March 11, 2008 5:20 PM

This is just great, absolutely fabulous!

Posted by: Jude at March 13, 2008 1:21 AM

Mr Taylor
I can not explain my happyness.
Many thanks from Buenos Aires, Argentina
And a big hug.

Posted by: Luis Alberto at March 22, 2008 10:41 PM

does any one how too work it on phpbb3???

Posted by: RAJA YALAMANCHILI at July 30, 2008 9:35 PM

"If you are using PHP Nuke 7.9 and the integrated forum, the viewtopic_body.tpl file to edit should be the one in root of your site, in \themes\DeepBlue\forums (if you don't then your site will appear to be doing nothing at all). This only applies to PHP Nuke installs that use the forum module pre-configured. You would also need to adjust the code for each template used. I had to do this to http://www.elitebattlemasters.com I hope it helps!!

Fish-Guy"


thank you Fish-Guy...:):)

Posted by: Judy at September 27, 2008 8:09 PM

Thanks !!!
=)

Posted by: Carlos Augusto Lippel at November 20, 2008 3:46 PM

thnx

Posted by: Mahendra at January 27, 2009 9:54 AM

thanks man , hope it works....

Posted by: suganindia at August 12, 2009 8:16 AM

Thanks very much - I am going to add this to my support forum.

Posted by: Jordan at January 6, 2010 2:14 PM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement



Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2010 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.