Mediawiki Google PlusOne

From YobiWiki
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.


# Google Plus
$wgHooks['BeforePageDisplay'][]  = 'fnPlusOneJavaScript';
function fnPlusOneJavaScript( $out )
  $script = '<script type="text/javascript" language="javascript" src="">'
            . '</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;