Create adsense ads for mediawiki

From OpenTutorial

Jump to: navigation, search
Don't Waste My Time...
Let's get something straight. I'm a jerk. I'm obnoxious. And I'm lazy.
And I don't care, because I'm FILTHY RICH.

Find Out More...

How to Create a Google Adsense Extension for Mediawiki - kdm adsense is a Mediawiki Extension which adds a new <adsense> tag which allows you to add an adsense button to your wiki pages.

Contents

[edit] The Code

<?php
/* kdm adsense WikiMedia extension
** kdm (c) 2006
** Add the following line to LocalSettings.php
** require_once('extensions/kdm/kdm_adsense.php');  // kdm adsense Extension
*/

$wgExtensionFunctions[] = "wfadsenseExtension";

function wfadsenseExtension() {
   global $wgParser;
   global $kdm_adsense;
   # register the extension with the WikiText parser
   $wgParser->setHook( "adsense", "renderadsense" );

   $kdm_adsense = array();
   # CHANGE THE LINES BELOW TO REFLECT TO YOUR ADSENSE ADS!!! (there's no limit on the number of ads you define)
   $kdm_adsense[1] = '
<script type="text/javascript">
<!-- google_ad_client = "pub-4452383759975954";
google_ad_width = 468;
google_ad_height = 15;
google_ad_format = "468x15_0ads_al";
google_ad_channel ="6604887705";
google_color_border = "FFFFCC";
google_color_bg = "FFFFFF";
google_color_link = "000000";
google_color_url = "336699";
google_color_text = "333333";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
';


   $kdm_adsense[2] = '
<script type="text/javascript"><!--
google_ad_client = "pub-4452383759975954";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel ="1172885669";
google_color_border = "C3D9FF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
';
}

# The callback function for converting the input text to HTML output
function renderadsense( $input, $argv ) {
   global $kdm_adsense;
   global $wgAuth;
   $pos_space=strpos($input,' ');
   if (!$pos_space) {
       if (is_numeric($input)) {  // format <adsense>number</adsense>
           $part1 = $input;
           $part2 = '';
           if (!$kdm_adsense[$part1]) {
               print_r ('warning, specified adsense ad not found, defaulting to button 1');
               $part1 = 1;
               $part2 = $input;
           }
       } else {                   // format <adsense>text</adsense> & format <adsense></adsense>
           $part1 = 1;
           $part2 = $input;
       }
   } else {                       // format <adsense>number text</adsense>
       $part1=substr($input,0,$pos_space);
       $part2=substr($input,$pos_space+1);
       if (is_numeric($part1)) {
           if (!$kdm_adsense[$part1]) {
               print_r ('warning, specified adsense ad not found, defaulting to button 1');
               $part1 = 1;
           }
       } else {                 // format <adsense>text</adsense>
           $part1 = 1;
           $part2 = $input;
       }
   }
   $form=$kdm_adsense[$part1];
   // if the kdm interface is available, then use the clean function which is defined there, 
   // otherwise just clean the necessities...
   if (class_exists ('kdm')) {
       $input = $wgAuth->kdm->kdm->clean_value ($part2);
   } else {
       $part2 = str_replace( ">"            , ">"         , $part2 );
       $part2 = str_replace( "<"            , "<"         , $part2 );
       $part2 = str_replace( "\""           , """         , $part2 );
       $part2 = str_replace( "!"            , "!"         , $part2 );
       $part2 = str_replace( "'"            , "'"         , $part2 );
       $input = $part2;
   }
   $output = '<table border=0><tr><td>'.$form.'</td><td valign=center>'.$input.'</td></tr></table>';
   return $output;
}
?>


[edit]  !!WARNING!!

If the text that you place between the single quotes in the $kdm_adsense[1] variable has any single quotes in it then you must escape them otherwise IT WILL NOT WORK.

You will get an error like similar to "Parse error: parse error, unexpected '}' in /home/content/O/p/e/OpenTutorial/html/extensions/kdm/kdm_adsense.php on line 21"

Example:

  • $kdm_adsense[2] = '<form action="https://... - it\'s fast, free and secure!"></form>'
    • works just fine
  • $kdm_adsense[2] = '<form action="https://... - it's fast, free and secure!"></form>'
    • will crash your wiki


[edit] Installation

  • create a directory kdm in the extensions directory
  • place kdm_adsense.php in the extensions/kdm directory (see code below)
  • open the file LocalSettings.php, search for:
?>

and change this to:

    require_once('extensions/kdm/kdm_adsense.php');  // kdm adsense Extension
?>
  • in the function wfadsenseExtension, place the code for your adsense-buttons in the variable $kdm_adsense.


[edit] Usage

  • the tag <adsense> is translated to the button defined in $kdm_adsense[1].
  • the tag <adsense>2</adsense> is translated to the button defined in $kdm_adsense[2].
  • the tag <adsense>2 description</adsense> is translated to the button defined in $kdm_adsense[2], additionaly the text 'description' is placed next to the adsense button.
  • the tag <adsense>description</adsense> is translated to the button defined in $kdm_adsense[1], additionaly the text 'description' is placed next to the adsense button.


[edit] Donate

If you find this extension usefull, then perhaps you can donate a few bucks? ;)


[edit] Example

<adsense></adsense> Produces (Note: Sometimes this doesn't always how up, I don't know why)

<adsense>2</adsense> Produces


[edit] Inspiration

This extension was inspired by the IpbWiki Paypal Extension


[edit] See also


[edit] Bibliography

  • ( ISBN 0470043997 ) Wikis For Dummies (For Dummies (Computer/Tech))) "Wikis For Dummies (For Dummies (Computer/Tech))" by Dan Woods
  • ( ISBN 020171499X ) The Wiki Way: Quick Collaboration on the Web) "The Wiki Way: Quick Collaboration on the Web" by Bo Leuf, Ward Cunningham
  • ( ISBN 3540259953 ) Wiki: Web Collaboration) "Wiki : Web Collaboration" by Anja Ebersbach, Markus Glaser, Richard Heigl, G. Dueck (Foreword), Andrea Adelung (Translator)


Bookmark this page on your favorite bookmarking site.
del.icio.us Reddit YahooMyWeb NewsVine Furl De.lirio.us digg blinkbits BlinkList blogmarks connotea Fark feedmelinks LinkaGoGo Ma.gnolia Netvouz RawSugar Shadows Simpy Smarking Spurl TailRank Wists
Personal tools


Promote OpenTutorial
Toolbox

feeds