Difference between revisions of "CentOS 7.x Configure mediawiki to display google adsense advertizements"

From Notes_Wiki
(Created page with "<yambe:breadcrumb self="Configure mediawiki to display google adsense advertizements">CentOS_7.x_Mediawiki|Mediawiki</yambe:breadcrumb> =CentOS 7.x Configure mediawiki to disp...")
 
m
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
<yambe:breadcrumb self="Configure mediawiki to display google adsense advertizements">CentOS_7.x_Mediawiki|Mediawiki</yambe:breadcrumb>
[[Main Page|Home]] > [[CentOS]] > [[CentOS 7.x]] > [[CentOS 7.x Web Based Tools|Web Based Tools]] > [[CentOS 7.x Mediawiki|Mediawiki]] > [[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.
==Debug ads not getting displayed issue==
For ads to get displayed the site need to go through:
* Review - https://support.google.com/adsense/thread/24997874?hl=en
* Then ads.txt for approved site needs to be placed in document_root
* Then auto ads need to be enable for site using google.com/adsense
Each of these steps might take a few days for google to complete from their side.  Once all three steps are done, the ads should get displayed and there might be corresponding revenue also.
==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 22:
# 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 44:
'''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 59: Line 75:




<yambe:breadcrumb self="Configure mediawiki to display google adsense advertizements">CentOS_7.x_Mediawiki|Mediawiki</yambe:breadcrumb>
==Setup mediawiki adsense extension==
 
'''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
# 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.
# Download mediawiki Adsense extension from https://www.mediawiki.org/wiki/Extension:Google_AdSense
# Extract downloaded extension files in ./extensions/ folder inside mediawiki source base folder
# Add following to localsettings.php
#:<pre>
#:: 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
#:</pre>
# Reload mediawiki and test.  There should be "Advertizements" under tools on the left side.
 
 
 
[[Main Page|Home]] > [[CentOS]] > [[CentOS 7.x]] > [[CentOS 7.x Web Based Tools|Web Based Tools]] > [[CentOS 7.x Mediawiki|Mediawiki]] > [[CentOS 7.x Configure mediawiki to display google adsense advertizements]]

Latest revision as of 09:19, 25 August 2022

Home > CentOS > CentOS 7.x > Web Based Tools > Mediawiki > 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.


Debug ads not getting displayed issue

For ads to get displayed the site need to go through:

Each of these steps might take a few days for google to complete from their side. Once all three steps are done, the ads should get displayed and there might be corresponding revenue also.


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 > CentOS 7.x Configure mediawiki to display google adsense advertizements