Manual:Interface/Sidebar/Hacks

From MediaWiki.org
Jump to navigation Jump to search

This page lists hacks that used to be mentioned in Manual:Interface/Sidebar.

Change sidebar content when logged in (PHP)[edit]

In your LocalSettings.php file, use the SkinBuildSidebar hook to modify the sidebar. Use the following function to check for $wgUser->isLoggedIn() and call a different system message than sidebar.

$wgHooks['SkinBuildSidebar'][] = 'lfHideSidebar';
/**
 * Show a different sidebar for anonymous users.
 *
 * $skin Skin object
 * $bar array Contains the array items, out of which the sidebar will be created.
 * @return true
 */
function lfHideSidebar( $skin, &$bar ) {
  global $wgUser;
  // Hide sidebar for anonymous users
  if ( !$wgUser->isLoggedIn() ) {
    // Shows a special anonymous sidebar.
    $bar = array(
      // Returns the message text of that sidebar with only {{-transformation done.
      // Setting array keys "text" and "id"; array keys "href" and "active" stay unset.
      'anon' => array(
        'text' => wfMessage( 'anon_sidebar' )->inContentLanguage()->text(),
        'id' => 'anon-info',
      )
    );
  } else {
    // No changes, just display the sidebar as usual.
  }
  return true;
}

Then in your Wiki, go to MediaWiki:anon_sidebar and create your new sidebar.

Allow wiki markup[edit]

Many people on Support Desk have been asking about how to put arbitrary wikitext into the sidebar. Take the code from the above example. The text() function will return your page content unescaped, only {{-transformation is done. Instead of using the text() function, you can use the parse() function. Your page content will then be fully parsed from wikitext to HTML. Instead of

	'text' => wfMessage( 'anon_sidebar' )->inContentLanguage()->text(),

use this:

	'text' => wfMessage( 'anon_sidebar' )->inContentLanguage()->parse(),

That will allow your sidebar article to be proper wikitext.

Content of MediaWiki:Sidebar[edit]

In your sidebar, you will need to include the surrounding HTML tags to ensure the portlet is styled correctly. Your MediaWiki:Anon Sidebar page content will need to look something like the following example after the change:

<div class="portal" role="navigation" id='p-support' aria-labelledby='p-support-label'>
<h3 id='p-navigation-label'>Navigation</h3>
<div class='body'>
*[[Main Page]]
*[[Community portal]]
*[{{fullurl:Special:Recentchanges}} Recent changes]
*[[Sandbox]]
*[[Help]]
</div>
</div>