Skip to content


Google adsense in MediaWiki 1.13.0

Here is how we set up Google Adsense to show in MediaWiki 1.13.0

1. Force the monobook skin for everyone (unless you want to edit every skin…) by putting this setting in LocalSettings.php:

$wgSkipSkins = array(“chick”, “cologneblue”, “myskin”, “nostalgia”, “simple”, “standard”, “modern”);

2. In your skins folder, create a file within a subfolder (for instance “adsense/adsense_vertical.php”) with the following content (edit with your own Google Adsense code, also, make sure to choose a VERTICAL ad, since we are adding code in the left sidebar!):

<div id=”v-g-ads” class=”portlet”>
<h5>Google ads</h5>
<div class=”pBody”>
<script type=”text/javascript”><!–
google_ad_client = “pub-9471398824076666”;
/* 120×240 for ZenGamers 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>

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
}
if(!strstr($_SERVER[‘REQUEST_URI’], “Special:”) &&
!strstr($_SERVER[‘REQUEST_URI’], “action=submit”) &&
!strstr($_SERVER[‘REQUEST_URI’], “action=edit”) &&
!strstr($_SERVER[‘REQUEST_URI’], “Template:”)) include(“adsense/adsense_vertical.php”);
}

4. Upload the stuff (don’t forget there are 2 files to upload, not just one)… and wait for Adsense to crawl your pages πŸ˜‰

Posted in MediaWiki, web development.


5 Responses

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

  1. Anonymous says

    You hack will break when someone uses interlanguage links though. A better approach is to create and call a function, in the manner that the language box is originally used.

  2. John says

    Oh, no it won’t…ignore my comment from a few minutes ago, I see what you did there. Still better to create your own function instead of piggybacking like that though. πŸ˜‰

  3. patheticcockroach says

    Yeah I shouldn’t have quoted that much text (or should have quoted it to say “find this code, and edit after it”), it’s making things confused… Still, I don’t know how to create a proper and clean function for this, that’s why I did it this way.
    I suppose I could also make an extension, but I’m not good enough with all this hook system. And since my edits take at most a minute to implement, I’m too lazy to search for a cleaner way. But feel free to post it if you have one πŸ™‚

  4. Ajay says

    awesome – but the ads show up only on the main page. how can i show it on all pages?

  5. patheticcockroach says

    What you say is weird, because with this very code, the ads show up everywhere *except* on special pages, templates and when editing or submitting a page (this is for compliance with the Google Adsense TOS, which states that you can’t put their ads on a page which doesn’t have some “real” content), as you can see on http://www.wiki4games.com (I copy/pasted the code in this post from that wiki, as a backup).



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