How to control display of Google Adsense ads in IPB

Displaying Google Adsense ads on your IPB board is easy enough. Just copy and paste the code into your skins as you like. But this method doesn’t lend itself easily to updates, especially if you have several skins installed for your members to use. You have to update each skin every time you want to change the position of the ads or the ad code changes. I use a different method that allows for more easy control of ad placement and who sees what ads that I thought I’d share.

This method allows you to maintain code in just one place (two if you also use the IBP Portal), so is much easier to make updates. It also allows you to control who sees what ads, based on member group. This is especially useful if you offer subscriptions or want to display all or certain ads to anonymous visitors, but others or none to registered members or members in specific groups. I’ve use this method on Belchfire.net since IPB 1.3. The instructions below are for 2.04 but will also work for 2.1 with one minor change (see here).

You’ll need shell access or FTP access to your IPB files and a text editor, perferrably one with syntax highlighting of PHP files. If you have shell access, use your favourite text editor (I use VIM). If using FTP and will be downloading files, try the free PSPad text editor.

Step 1
Step 2
Step 3
Step 4
Optional stuff
Putting it all together
If something goes wrong
Conclusion

Step 1:

Backup your /sources/classes/classdisplay.php file. Copy it to classdisplay.php.backup or similar so can be restored easily if things don’t work out.

Step 2:

Open classdisplay.php in a text editor. Scroll down past function dooutput and find:

 

//-----------------------------------------
// Add in task image?
//-----------------------------------------

Directly above this, paste the following:

 

//-----------------------------------------
// Banner Functions
//----------------------------------------- 

if ($ibforums->member['id'] == 0)
{
$googlebanner = "<!-- GOOGLE BANNER --><!-- GOOGLE BANNER -->";
} else {
$googlebanner = "";
}

Now paste your Google Adsense code between <!– GOOGLE BANNER –> and <!– GOOGLE BANNER –>. Final part to this step, and the most important, is to now ‘escape’ the double-quotes in your Google Adsense code. This means, find each ” character and add a backslash in front of it like so it now looks like \”. I find it easiest to copy your Google Adsense code into Notepad first, then do a search and replace to search for ” and replace with \”. Then copy the whole thing between <!– GOOGLE BANNER –> and <!– GOOGLE BANNER –>. In the end, the whole thing should look like this (with your ad code in place of mine):

 

//-----------------------------------------
// Banner Functions
//----------------------------------------- 

if ($ibforums->member['mgroup'] == 0)
{
$googlebanner = "<!-- GOOGLE BANNER -->
<script type="text/javascript"><!--
google_ad_client = "pub-3642237981785874";
google_alternate_ad_url = "http://as.casalemedia.com/sd?s=54186&f=1";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "FF4500";
google_color_bg = "FFEBCD";
google_color_link = "DE7008";
google_color_url = "E0AD12";
google_color_text = "000000";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- GOOGLE BANNER -->";
} else {
$googlebanner = "";
}

Step 3:

Still within class_display.php, scroll down just past:

 

//-----------------------------------------
// Add in task image?
//-----------------------------------------

and find all the lines that start with $ibforums->skin[’wrapper’] = strreplace(. Find the last line in this group, the one with <% LOFIVERSION %>
in it, and below it add the following new lines:

 

// Banner Ads 

$ibforums->skin['_wrapper'] = str_replace( "" , $googlebanner , $ibforums->skin['_wrapper']);

The line starting with $ibforums should be all on one line, not line-wrapped as may appear here due to my blog skin. When done, save the file and upload it to your web server, overwriting your existing /sources/classes/class_display.php file.

Step 4:

Login to your Admin CP and click Skins & Templates –> Skin Manager –> (skin name) –> Edit Root Skin Template HTML –> All Global HTML –> globalboardheader. In the editor window that appears, add the following to the very end of all the other code:

<% GOOGLE BANNER %>

Click the ‘Save Template Bit(s)’ button.

That’s it! Now, visit your IPB board as an anonymous visitor and you should see your Google banner directly below your submenu nav bar. Login now as a member and the banner will be gone. w00t! Add <% GOOGLE BANNER %> to all your other IBP skins as you like. If/when you ever need to update your Google Adsense code in the future, all you do is update it in your /sources/classes/class_display.php file and it will automatically show the updated ad in all your skins.

Now all the optional stuff…

  • You may not want your Google ad to appear under the submenu nav bar. If you want it to appear, for example, at the very top of your page, remove <% GOOGLE BANNER %> from global_board_header and enter it into the Edit Root Skin Board Header & Footer Wrapper for your skin. Experiment with placement until you get it where you want. Either directly below the BODY tag or perhaps within the first designwrap or ipbwrapper DIV tags.You may also want to align the ad as appropriate for that particular skin. In this case, you can enslose <% GOOGLE BANNER %> within it’s own DIV and assign whatever style to it that you like. I prefer to define a new DIV ID and drop the style definition into my CSS for that skin. Do whatever you like - it’s up to you.
  • My example above shows the Google ad to only anonymous visitors (member[‘mgroup’] == 0). If you want to change this behaviour, determine the ID numbers for your member groups that you want to include or exclude from seeing your banners and change the arguments in the function using standard operators.You can determine the IDs of your member groups by logging into your Admin CP and clicking Users and Groups –> Manager User Groups. Then hold your mouse over the ‘Edit Group’ link in the table with your groups. Look at the URL displayed in the status bar at the bottom of your web browser. The last character in the URL is the mgroup ID (…&id=4 is mgroup 4). By default Admin is 4, Banned is 5, Guests is 2, Members is 3, Validating is 1, any custom groups you have created will be mgroup ID 6 and higher.For example, on Belchfire.net, I display my Google banner to all visitors except members of my Sponsoring Members group and the Admin group, like so:

    if (($ibforums->member[‘mgroup’] != 6) && ($ibforums->member[‘mgroup’] != 4))

    My member group Admin is mgroup 4 and Sponsoring Member is mgroup 6. The != means ‘not equal to’, or in other words, don’t display to these groups. Use ==, which means ‘equal to’, to display to that mgroup specifically. The && between the two mgroup statements means ‘and’. So if you have more member groups you want to exclude, simply add additional statements, separating each with &&.

  • Now here’s the really cool part. You can repeat each step above for any other ad programs you’re running. This will work for any ad network you use: Google, Fastclick, Clicksor, Casale, whatever. You can even repeat the above for different mediums within the same ad network, or even to allow for the display of multiple ads on the same page. You can also define different Google adcodes for the same medium but using different colour schemes to match your themes.Let say you want to also use Google’s new Link Unit ads in additional to your regular banner ad. Just add a new if/else section to class_display.php as above, replace the banner ad code with the Link Unit ad code (don’t forget to escape your double-quotes!), change the variable name to perhaps $googlelinkunit and the IPB Skin wrapper replacement to perhaps <% GOOGLE LINK UNIT %>. Then add <% GOOGLE LINK UNIT %> to your skins as you like. The variable names are inconsiquental, they just have to match. If using Fastclick skyscraper ads, you could name your variable $fastclickskyscraper. Or use a common prefix to make them easy to find later, like $myfunkybannercode_googleleaderboard, $myfunkybannercode_clicksorpopup, etc.You can even repeat each invidual ad with different color schemes so they match your forums skins. Just change the variables and skin replacement text for each to include a unique name, such as $googlebanner_blue and $googlebanner_red, and in the skin replacements, <% GOOGLE BANNER BLUE %> and <% GOOGLE BANNER RED %>. You get the idea…
  • You can also enter all the variables and ad codes you want into class_display.php without entering the skin replacement text into your skin files. As you’ll see below in ‘Putting it all together’, I have more ad codes in my class_display.php file that are actually in my skin files. I add/remove/change them sometimes so once entered into class_display.php, I just switch them around as I like by adding the skin replacement text - it’s so easy.
  • I forgot to mention the else statement. You don’t actually need an else statement in the class_display.php file for your ads at all. If the mgroup of the visitor doesn’t match what you’ve defined, they simply won’t see anything where the ad would normally appear. The else statement allows you to define what those visitors will see if they aren’t a match. If there is nothing in the single quotes beside $variablename = after the } else {, nothing will appear. But you could enter any HTML or text you like here to display a thank you message, a system notice, another banner of your choosing - whatever. It’s entirely up to you. Just make sure you escape your double-quotes.

Putting it all together

Here’s what my class_display.php file looks like with variables for a number of ad networks and code but with the actual ad code and comments stripped out so you can see the format:

Oh, anything with // in front of it is a PHP comment. Add your own as you like to keep track of things. And again, all the $ibforums lines should be on their own line - not lined wrapped as may appear here due to my blog width.

 

//-----------------------------------------
// Banner Functions
//----------------------------------------- 

// Display these ads to all but members of the
// Sponsoring Members and Admin groups 

if (($ibforums->member['mgroup'] != 6) && ($ibforums->member['mgroup'] != 4))
{ 

$googlebannerwrapper = "BANNER CODE HERE";
$toolbarbannerwrapper = "BANNER CODE HERE";
$fastclickpopunderwrapper = "BANNER CODE HERE";
$fastclickinvuewrapper = "BANNER CODE HERE";
$fastclickmediumrectangle = "BANNER CODE HERE";
$fastclickintersticial = "BANNER CODE HERE";
$textadswrapper = "BANNER CODE HERE";
$casalepopunderwrapper = "BANNER CODE HERE";
$casaleskyscraperwrapper = "BANNER CODE HERE";
$casalerectanglewrapper = "BANNER CODE HERE";
$casaleleaderboardwrapper = "BANNER CODE HERE";
$referralbuttons = "BANNER CODE HERE"; 

} else { 

// If is not member of these groups, display nothing 

$googlebannerwrapper = "";
$toolbarbannerwrapper = "";
$fastclickpopunderwrapper = "";
$fastclickinvuewrapper = "";
$fastclickmediumrectangle = "";
$fastclickintersticial = "";
$textadswrapper = "";
$casalepopunderwrapper = "";
$casaleskyscraperwrapper = "";
$casalerectanglewrapper = "";
$casaleleaderboardwrapper = "";
$referralbuttons = ""; 

} 

// Display these ads only to member groups
// Validating and Guests 

if (($ibforums->member['mgroup'] == 1 ) || ($ibforums->member['mgroup'] == 2))
{ 

$clicksorwrapper = "BANNER CODE HERE";
$mediaplazzaleaderwrapper = "BANNER CODE HERE"; 

} else { 

// Display nothing 

$clicksorwrapper = "";
$mediaplazzaleaderwrapper = ""; 

} 

//-----------------------------------------
// Add in task image?
//----------------------------------------- 

if ( time() >= $ibforums->cache['systemvars']['task_next_run'] )
{
$this->to_print .= "<!--TASK--><img src='{$ibforums->base_url}act=task'
border='0' height='1' width='1' /><!--ETASK-->";
} 

$ibforums->skin['_wrapper'] = str_replace( "<% CSS %>" , $css , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% JAVASCRIPT %>" , "" , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% TITLE %>" , $output_array['TITLE']
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% BOARD %>" , $this->to_print , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% STATS %>" , $stats , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% GENERATOR %>" , "" , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% COPYRIGHT %>" , $copyright , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% BOARD HEADER %>" , $this_header ,
$ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% BOARD FOOTER %>" , $this_footer ,
$ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% NAVIGATION %>" , $nav , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% SKINCHOOSER %>" , $skin_jump , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% LANGCHOOSER %>" , $lang_jump , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% QUICKSTATS %>" , $quickstats , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% LOFIVERSION %>" , $this->_get_lofi_link()
, $ibforums->skin['_wrapper']); 

// Banner Ads 

$ibforums->skin['_wrapper'] = str_replace( "<% GOOGLE BANNER %>" , $googlebannerwrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% TOOLBAR BANNER %>" , $toolbarbannerwrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% FASTCLICK POPUNDER %>" , $fastclickpopunderwrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% FASTCLICK INVUE %>" , $fastclickinvuewrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% FASTCLICK INTERSTICIAL %>" , $fastclickintersticial
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% FASTCLICK MED RECTANGLE %>" , $fastclickmediumrectangle
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% TEXTADS %>" , $textadswrapper , $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% CLICKSOR %>" , $clicksorwrapper ,
$ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% CASALE SKYSCRAPER %>" , $casaleskyscraperwrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% CASALE RECTANGLE %>" , $casalerectanglewrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% CASALE POPUNDER %>" , $casalepopunderwrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% CASALE LEADERBOARD %>" , $casaleleaderboardwrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% MEDIAPLAZZA LEADERBOARD %>" , $mediaplazzaleaderwrapper
, $ibforums->skin['_wrapper']);
$ibforums->skin['_wrapper'] = str_replace( "<% REFERRALBUTTONS %>" , $referralbuttons
, $ibforums->skin['_wrapper']);

And a sample skin globalboardheader skin file:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><% TITLE %></title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<% GENERATOR %>
<% CSS %>
<% JAVASCRIPT %>
<% CASALE POPUNDER %>
<% FASTCLICK INTERSTICIAL %>
</head> 

<body> 

<% FASTCLICK INVUE %> 

<div id="mygooglebanner">
<% GOOGLE BANNER %>
</div> 

<div id="ipbwrapper">
<% BOARD HEADER %>
<% MEMBER BAR %>
<% NAVIGATION %>
<!--IBF.NEWPMBOX-->
<% BOARD %> 

<div id="myfastclickmedrectangle">
<% FASTCLICK MED RECTANGLE %>
</div> 

<% BOARD FOOTER %>
<% STATS %>
<% COPYRIGHT %>
<% REFERRALBUTTONS %>
<% CLICKSOR %>
</div> 

</body>
</html>

And with DIV IDs defined in skin CSS, of course.

If something goes wrong…

If something goes wrong, meaning, you get a blank white page with an error message, check the code you entered into class_display.php. You’ve likely missed escaping a double-quote in the ad code, forgot to close the if/else statements with a } bracket, or are missing a semi-colon or something. The format must be as above. The syntax highlighting of your text editor, if you are using one as I suggest, will show you if you’ve missed something.

If your pages load fine but you don’t see the ad, log out and view your page as an anonymous visitor. Or, depending on how you defined your mgroup includes/excludes, login as a member of that specific group and see if the ad appears or does not appear, as the case may be. If you are logged in as a member and are still seeing the ad, ensure you have the correct member group ID number in your mgroup statements. You may have to login and log back out to test after making changes.

Also ensure the variable name matches in the if/else statement and in the skin wrapper replacement. You can’t use $googlebanner in one and $googlebanners in the other - ensure they match, and check for typos - could be missing a letter - it’s always something silly like that.

Ensure your lines aren’t wrapping. If you copied and pasted any examples directly from this article, ensure they didn’t wrap when you pasted them into your own file. Happens with the $ibforums lines the most as they are really long. Just backspace the wrapped portion of the line so it moves up behind the first part of the line and that should fix things.

If all that is correct, ensure the string replacement text matches in class_display.php and in your skin template bits.

Failing all this, if things STILL don’t work, you can always replace your class_display.php with the backup you made in Step 1. That will get things working again as before. Just remember to also remove the text string replacements you entered into your skin bits.

IPB 2.1.x Changes

The above code and instructions work for IPB 2.0.x. If you have upgraded to IPB 2.1.x, there are just two changes you have to make. Where ever I reference $ibforums->member[‘mgroup’], replace with $this->ipsclass->member[‘mgroup’]. And replace $ibforums->skin[’wrapper’] with $this->ipsclass->skin[’wrapper’]. That’s it!

Conclusion

As I said at the beginning, the advantage to this method is that, after you initially set things up, you don’t need to edit all your skins if you ever update or change your ad code. Just update it in class_display.php and it automatically updates in all your skins. Bonus is you can control who sees banners and who does not, based on member group. An added bonus is you can control where and how your banner ads appear on a per-skin basis, by changing the location of the skin wrapper replacement and defining (optionally), different CSS styles for each skin and even each ad (by using DIV IDs). So I hope you find this useful and give it a try.

The downside to using this method is that if you ever upgrade IPB, you have to ensure that you backup your classdisplay.php file and then re-add everything back into the new file after upgrading. And this assumes you would even use classdisplay.php (in 1.3 and earlier it was the functions.php file). It could change - you never know.

Next time I’ll show you how to do the same thing for the IPB Portal. It’s a little different because the IPB Portal doesn’t use the class_display.php file - it uses it’s own class file. Custom skin bits are also required to make things work correctly.

One Response to “How to control display of Google Adsense ads in IPB”

  1. Belchfire Search Engine Marketing · Invision - Monetize your IPB Lo-fi Version Says:

    […] you’ve read and implemented my method for controlling display of Google Adsense ad in Invision Power Board, you’re already reaping the benefits. But have we forgotten something? Yes, I believe we […]