How To Hide MediaWiki Sidebar From Visitors?

You can suppress the display of the sidebar from anonymous and un-logged-in users on your MediaWiki website. This can be done by creating a new PHP file in extensions folder then including the code in LocalSettings.php file.

Login to your MediaWiki website’s file manager or FTP, navigate to /extensions directory located in your site’s root and create a new folder /HideSidebar.

Open the folder, create a new PHP file with name HideSidebar.php and paste the following code:

<?php

if ( !defined( 'MEDIAWIKI' ) ) {
	echo "Not a valid entry point";
	exit( 1 );
}

$wgExtensionCredits['other'][] = array(
	'path' => __FILE__,
	'name' => 'HideSidebar',
	'version' => '1.0.1',
	'author' => 'Jlerner',
	'url' => 'https://www.mediawiki.org/wiki/Extension:HideSidebar',
	'description' => 'Allows to hide the sidebar from anonymous users',
);

$wgHooks['SkinBuildSidebar'][] = 'efHideSidebar';
 
function efHideSidebar($skin, &$bar) {
        global $wgUser;
        // Hide sidebar for anonymous users
        if (!$wgUser->isLoggedIn()) {
                $url = Title::makeTitle(NS_SPECIAL, 'UserLogin')->getLocalUrl();
                $bar = array(
                        'navigation' => array(
                                array('text'   => 'Login',
                                      'href'   => $url,
                                      'id'     => 'n-login',
                                      'active' => '')
                        )
                );
        }
        return true;
}

Now open LocalSettings.php file and add this line of code:

require_once "$IP/extensions/HideSidebar/HideSidebar.php";

Save changes and you are done.

Leave a Reply