From Notes Wiki
Jump to: navigation, search
m
m
Line 2: Line 2:
 
=CentOS 7.x Configure mediawiki to display google adsense advertizements=
 
=CentOS 7.x Configure mediawiki to display google adsense advertizements=
  
 +
==Enable auto-ads using HeadScript extension==
 +
Google has created auto-ads which require putting only one JS script between head and /head elements.  That can be done via Headscript extension ( https://www.mediawiki.org/wiki/Extension:HeadScript ) which is very straightforward (Single PHP file) to setup and use.
 +
 +
On local machine this usage led to undefined variable type warning.  So commenting entire $wgExtensionCredits[$type][] variable with php /* */ comments seems to have solved the problem.
 +
 +
 +
==Display fixed adds in header and footer using site hooks==
 
#[[Create Google Adsense Account]]
 
#[[Create Google Adsense Account]]
 
#Optionally, [[Link adsense account to youtube channel]]
 
#Optionally, [[Link adsense account to youtube channel]]
Line 7: Line 14:
 
# Paste script code in Localsettings.php file to display google adsense in mediawiki  
 
# Paste script code in Localsettings.php file to display google adsense in mediawiki  
  
==Header advertizement script==
+
 
 +
===Header advertizement script===
 
<pre>
 
<pre>
 
$wgHooks['SiteNoticeAfter'][] = function(&$siteNotice, $skin) {
 
$wgHooks['SiteNoticeAfter'][] = function(&$siteNotice, $skin) {
Line 28: Line 36:
 
'''Remember to replace script in above code with script available in your google adsense account related to the ad'''
 
'''Remember to replace script in above code with script available in your google adsense account related to the ad'''
  
==Footer advertizement script==
+
===Footer advertizement script===
 
<pre>
 
<pre>
 
$wgHooks['SkinAfterContent'][] = function(&$data, $skin) {
 
$wgHooks['SkinAfterContent'][] = function(&$data, $skin) {
Line 61: Line 69:
 
==Setup mediawiki adsense extension==
 
==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.'''
+
'''Latest extension updated for mediawiki 1.31+.  However due to missing extension.json file it does not works'''
  
 
# Create account on www.google.com/adsense    It might take 1 or 2 days for account to be activated
 
# Create account on www.google.com/adsense    It might take 1 or 2 days for account to be activated

Revision as of 12:19, 14 January 2020

Home > CentOS > CentOS 7.x > Web based tools > Mediawiki > Configure mediawiki to display google adsense advertizements

CentOS 7.x Configure mediawiki to display google adsense advertizements

Enable auto-ads using HeadScript extension

Google has created auto-ads which require putting only one JS script between head and /head elements. That can be done via Headscript extension ( https://www.mediawiki.org/wiki/Extension:HeadScript ) which is very straightforward (Single PHP file) to setup and use.

On local machine this usage led to undefined variable type warning. So commenting entire $wgExtensionCredits[$type][] variable with php /* */ comments seems to have solved the problem.


Display fixed adds in header and footer using site hooks

  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

Latest extension updated for mediawiki 1.31+. However due to missing extension.json file it does not works

  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.


Home > CentOS > CentOS 7.x > Web based tools > Mediawiki > Configure mediawiki to display google adsense advertizements