Quantcast
Viewing all articles
Browse latest Browse all 9

How to add adsense to Mediawiki

Due to the increasing popularity of my Samsung Wiki site, SammyWiki.com I took the decision to try and reclaim some of the hosting fees.

Unfortunately as sites like this become more popular the cost of running them increases too.  Google’s adsense becomes a simple way to generate a few pounds each month to help offset the increasing bandwidth costs. And hopefully as traffic continuous to increase that revenue should grow too!

Adding Adsense to Mediawiki

Unless any major changes are made this guide should continue to work for new release of the wiki software. Here is what I wanted to achieve:

  • Show adsense only on genuine article pages. So ads should not show when making a page edit, or viewing special pages etc. As per Adsense terms and conditions, the ads should only show on the real content pages
  • Hide Adsense ads from logged in users. I don’t want regular readers to get bothered by the ads, I don’t want to tsee them myself (and risk an accidental click!). So a nice way to encourage people to sign up and to reward the regular wiki editors is to ensure they don’t see the ads. I want to show ads to unlogged in people only who will most likely just be passing through the website whilst looking for more info – these are the people worth showing ads to!
  • Position the unit within the content. I wanted to show the ad units at the top right of the main wiki page content area, just below the main page title

Altering the Wiki Skin

So to get the ads to show I made a small change to the monobook.php file that you’ll find in your skins folder. Obviously if you are using a different skin then you will need to make the changes in the appropriate file!

I wanted the ads to show after the main page heading so, I started editing below the H1 tags. Look for a row that starts with:

<h1 id="firstHeading" class="firstHeading">

And then we can add our new code on the following line.

So the first challenge is to test for genuine wiki article pages, this can be done with the following function:

$wgOut->isArticle()

The next part is to test for logged in users, and again there is a handy function to do this for us:

$wgUser->isLoggedIn()

And finally we just need to add in our adsense code to display on these pages. I was keen that the adsense units showed on the right hand side of the page, this is easily done by wrapping in DIV tags like so:

<div style="text-align:right">

So the final code looks like:

<h1 id="firstHeading" class="firstHeading"><?php $this->data['displaytitle']!=""?$this->html('title'):$this->text('title') ?></h1><?php

global $wgOut;

if($wgOut->isArticle()) {

global $wgUser;

if(!$wgUser->isLoggedIn()) {

echo '<div style="text-align:right"><script type="text/javascript"> <!--

google_ad_client = "pub-YOURIDHERE";

/* 468x60 */

google_ad_slot = "3835222762";

google_ad_width = 468;

google_ad_height = 60;

//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>';}} ?>

And there you go, that’s how to add adsense units to the right hand side of wiki pages just below the main page title! Simples. Here’s hoping it manages to generate enough to keep the site going as it grows!

To find out more about the Sammy Wiki site, check out the samsung wiki forum here.


Viewing all articles
Browse latest Browse all 9

Trending Articles