Skip to content


Google Adsense in MediaWiki 1.14.0

Update (2011-07-17): the easiest way, rather than modifying the source code, is to use the PCR GUI Inserts MediaWiki extension.

Here is how we set up Google Adsense (and more generally, a customized ad area which will show Google Adsense on pages where it is allowed, and another ad on other pages) to show in MediaWiki 1.14.0:

1. Force the monobook skin for everyone (unless you want to edit every skin…) by putting this setting in LocalSettings.php:
$wgAllowUserSkin = false;
NB: this setting is new to MediaWiki 1.14! If you have an older version, either upgrade or see our previous tutorial

2. In your skins folder, create a file within a subfolder (for instance “0custom/ad_vertical.php”) with the following content (edit with your own Google Adsense code as well as your own alternative ad; also, make sure to choose a VERTICAL ad, since we are adding code in the left sidebar!):
<?php
if(!strstr($_SERVER['REQUEST_URI'], "Special:") &&
!strstr($_SERVER['REQUEST_URI'], "action=submit") &&
!strstr($_SERVER['REQUEST_URI'], "action=edit") &&
!strstr($_SERVER['REQUEST_URI'], "Template:"))
echo '<div id="v-g-ads" class="portlet">
<h5>Google ads</h5>
<div class="pBody">
<script type="text/javascript"><!--
google_ad_client = "pub-9471398824076666";
/* 120x240 for Wiki4Games sidebar */
google_ad_slot = "3512091103";
google_ad_width = 120;
google_ad_height = 240;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</div>';
else echo '<div id="v-g-ads" class="portlet">
<h5>Ads</h5>
<div class="pBody">
<a href="http://proxify.com/r/?s=1320"><img src="http://proxify.com/i/info-110x110.gif" width="110" height="110" alt="disable proxy" border="0"></a>
</div>
</div>';
?>

3. Edit MonoBook.php as follow:
Replace:
function languageBox() {
if( $this->data['language_urls'] ) {
?>
<div id="p-lang" class="portlet">
<h5><?php $this->msg('otherlanguages') ?></h5>
<div class="pBody">
<ul>
<?php foreach($this->data['language_urls'] as $langlink) { ?>
<li class="<?php echo htmlspecialchars($langlink['class'])?>"><?php
?><a href="<?php echo htmlspecialchars($langlink['href']) ?>"><?php echo $langlink['text'] ?></a></li>
<?php } ?>
</ul>
</div>
</div>
<?php
}
}

With:
function languageBox() {
if( $this->data['language_urls'] ) {
?>
<div id="p-lang" class="portlet">
<h5><?php $this->msg('otherlanguages') ?></h5>
<div class="pBody">
<ul>
<?php foreach($this->data['language_urls'] as $langlink) { ?>
<li class="<?php echo htmlspecialchars($langlink['class'])?>"><?php
?><a href="<?php echo htmlspecialchars($langlink['href']) ?>"><?php echo $langlink['text'] ?></a></li>
<?php } ?>
</ul>
</div>
</div>
<?php
}
include("0custom/ad_vertical.php");
}

NB: In fact we’re just adding the include at the end of the function.

4. Upload the stuff (don’t forget there are 2 files to upload, not just one), and you’re done 🙂

Posted in MediaWiki, web development.


7 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. acnecare says

    i am a beginner in monetizing my blog with Adsense. i still need to learn more about the tips and tricks on Adsense. hopefully i might be able to earn a decent amount of cash on Adsense.

  2. melatoninplus says

    i really love Google Adsense. i could earn a decent amount of cash with this make money online program from Google.

  3. Anna Leah says

    Adsense is the best paying make money online program on the internet. I just wish that i could increase my website traffic to high levels so that i could earn more money on Adsense.

  4. Cathryn says

    i use Adsense on my blogs and websites, i can say that Adsense is the best make money online program on the internet.

  5. patheticcockroach says

    If you spammers don’t get more insightful you’ll never manage to get your Viagra links published along with your comment…

  6. Jainee says

    i love Google Adsense, it allows me to make money on my personal blog and some of my health related sites.

  7. TJ says

    Hi. I’m gonna give this a whirl right now. I’m using mediawiki 1.17 and I’ve been looking for and adsense solution. Hopefully this is it!

    -TJ



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA