Mediawiki Google PlusOne

From YobiWiki
Revision as of 10:28, 1 September 2011 by <bdi>PhilippeTeuwen</bdi> (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is just a little hack to add the Google +1 button to a mediawiki installation. See technical notes on integrating Google +1 button for webmasters

Simply add the following snipped to your LocalSettings.php.

Remarks:

# Google Plus
$wgHooks['BeforePageDisplay'][]  = 'fnPlusOneJavaScript';
function fnPlusOneJavaScript( $out )
{
  $script = '<script type="text/javascript" language="javascript" src="https://apis.google.com/js/plusone.js">'
            . '</script>';
  $out->addScript( $script);
  return true;
}

$wgHooks['ArticleViewHeader'][] = 'fnPlusOneAnchor';
function fnPlusOneAnchor( &$article, &$outputDone, &$pcache )
{
    global $wgOut;
    global $wgTitle;
    $wgOut->addHTML('<div align=right><g:plusone size="medium" href="' . $wgTitle->escapeLocalUrl() . '"></g:plusone></div>');
    return true;
}

And make sure you invalidate the server cache. One easy way is to add the following to your LocalSettings.php to clean the cache whenever LocalSettings.php is edited:

$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );

From v1.17 of mediawiki you can use this instead:

$wgInvalidateCacheOnLocalSettingsChange = true;