CentOS 7.x Configure mediawiki to display google adsense advertizements

From Notes_Wiki
Revision as of 15:11, 28 January 2019 by Saurabh (talk | contribs)

<yambe:breadcrumb self="Configure mediawiki to display google adsense advertizements">CentOS_7.x_Mediawiki|Mediawiki</yambe:breadcrumb>

CentOS 7.x Configure mediawiki to display google adsense advertizements

  1. Create Google Adsense Account
  2. Optionally, Link adsense account to youtube channel
  3. Create appropriate ad unit for displaying ads on mediawiki and get script code.
  4. Paste script code in Localsettings.php file to display google adsense in mediawiki

Header advertizement script

$wgHooks['SiteNoticeAfter'][] = function(&$siteNotice, $skin) {
        $siteNotice .= <<< EOT
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- XXX -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxxx"
     data-ad-slot="xxxxxx"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOT;
        return true;
};

Remember to replace script in above code with script available in your google adsense account related to the ad

Footer advertizement script

$wgHooks['SkinAfterContent'][] = function(&$data, $skin) {
        global $myAdCode;
        $data .= '<div style="text-align:center;">';
        $data .= <<< EOT
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- XXX -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxx"
     data-ad-slot="xxxxx"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOT;
 
        $data .= '</div>';
        return true;
};

Remember to replace script in above code with script available in your google adsense account related to the ad


Refer:


Setup mediawiki adsense extension

Does not works with mediawiki 1.31 and Latest Google ads due to change in javascript syntax used by google. Since the extension is not under active maintenance, this should be avoided anyway.

  1. Create account on www.google.com/adsense It might take 1 or 2 days for account to be activated
  2. After activation go to Ads -> Ad units and create a new "Ad unit". Copy the script code displayed at end for valuable parameters required later.
  3. Download mediawiki Adsense extension from https://www.mediawiki.org/wiki/Extension:Google_AdSense
  4. Extract downloaded extension files in ./extensions/ folder inside mediawiki source base folder
  5. Add following to localsettings.php
    require_once "$IP/extensions/GoogleAdSense/GoogleAdSense.php";
    $wgGoogleAdSenseClient = 'none'; // Client ID for your AdSense script (example: ca-pub-1234546403419693)
    $wgGoogleAdSenseSlot = 'none'; // Slot ID for your AdSense script (example: 1234580893)
    $wgGoogleAdSenseID = 'none'; //Name of ad unit created above. This would be part of Adsense script comment
    $wgGoogleAdSenseSrc = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'; //This is part of script
  6. Reload mediawiki and test. There should be "Advertizements" under tools on the left side.


<yambe:breadcrumb self="Configure mediawiki to display google adsense advertizements">CentOS_7.x_Mediawiki|Mediawiki</yambe:breadcrumb>