How to add Google AdSense to MediaWiki

From AJS.COM

Jump to: navigation, search

You'll see two ads on this page, if you're not using an adblocker. The first is at the top and the second is on the sidebar. However, if you view the history for this page, you won't see those ads.

Google has a terms of service agreement for AdSense that strictly does not allow the display of ads on pages which are restricted by robots.txt or authentication (unless you give Google's AdSense crawler an account). Since almost all MediaWiki sites exclude access to editing and other dynamic pages for crawlers, it's important that you not display ads on these pages. Here's how I do that:

In my MonoBook.php file, under the "skns" directory, I added this code just before the "language_urls" section of the sidebar:

       <?php global $wgOut, $wgArticle;
       if($wgOut->isArticle() && $wgArticle->isCurrent()) { ?>
       <script type="text/javascript">
       </script>
       <script type="text/javascript"
       src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
       </script><?php } ?>

The bulk of the code is just Google's AdSense boilerplate, but notice the use of the PHP code that checks "isArticle" and "isCurrent". That's the magic. This ad will never display on pages that are dynamically generated including past revisions, edit pages, etc.

Personal tools