Jump to content

Topic on Project:Support desk

Hide Sidebar when you're not logged in

3
Kitsuo23 (talkcontribs)

Hello,

I've set up a personal wiki, where i'm storing all kind of informations and documentations. I'm the only one using it so i created an account and disabled the registration button.

My problem is that my SideBar with article titles are visible at home page for all non registered users to see so my question would be : how to hide the sidebar for non-logged users ?

Thanks a lot for your time

Kitsuo23 (talkcontribs)

Up ?

Kitsuo23 (talkcontribs)

I found the solution (thank to https://sangkrit.net/how-to-hide-mediawiki-sidebar-from-visitors/):

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.