Jump to content

Topic on Project:Support desk

[RESOLVED] How can I hide tabs for users not logged in?

49
Summary last edited by Dinoguy1000 17:28, 9 August 2022 2 years ago

Please open a new thread if none of the solutions provided on this thread works for you, explaining what have you tried so far.

Holygamer (talkcontribs)

My Versions: MediaWiki: 1.16.5, PHP: 5.2.17, MySQL: 5.1.61

I'm using the Vector skin. I would like to hide the following tabs to people who do not have the right to use them. Is there any way to do this?

Discussion, View History, Edit, Arrow

This post was posted by Holygamer, but signed as Stoped and deleted all :Holygamer.

SVG (talkcontribs)

The following is simple CSS but it should work:

$wgHooks['SkinTemplateSetupPageCss'][] = 'wfHideVariousTabsFromAnonymous';

function wfHideVariousTabsFromAnonymous( &$hidetabcss ) {
	global $wgUser;

	if ( !$wgUser->isLoggedIn() ) {
		$hidetabcss .= 'li#ca-history, li#ca-viewsource, li#ca-edit, li#ca-talk, .vectorMenu { display: none; }';
	}

	return true;
}
Holygamer (talkcontribs)

Thanks but that only hides the tabs. The tabs are still there in the code. Is there any way to make the tabs not show in the first place and where should I put the code in the Vector skin? Thanks

Jasper Deng (talkcontribs)

I think there's an extension for this, but somehow I don't remember what it is.

SVG (talkcontribs)

There is no extension but there might be hooks that could be used. I'll look for it tomorrow.

Jasper Deng (talkcontribs)

What gives me this idea is that Hurricane Electric's wiki (found somewhere on he.net) succeeds in hiding all the special pages and all the tabs.

SVG (talkcontribs)
Holygamer (talkcontribs)

The DynamicTabs extension only works on the Monobook skin.

Jasper Deng (talkcontribs)

and the HE wiki uses Monobook. However, it looks like SVG might be able to make this a wholly new extension.

SVG (talkcontribs)

Can be catched up by:

$wgHooks['SkinTemplateNavigation'][] = 'fnHideVariousTabsFromAnonymousVector';

function fnHideVariousTabsFromAnonymousVector( SkinTemplate &$sktemplate, array &$links ) {
	// the old '$content_actions' array is thankfully just a
	// sub-array of this one
	fnHideVariousNamespaceTabsFromAnonymous( $sktemplate, $links['namespaces'] );
	fnHideVariousActionTabsFromAnonymous( $sktemplate, $links['views'] );
	return true;
}
Jasper Deng (talkcontribs)

It would be great if all users knew it; since they don't, an extension is a better way of presenting this to users. Perhaps include in Extension:Vector?

SVG (talkcontribs)

I'll write a short code, just not today because I'm currently in several spoken conversations (one after the other).

79.183.171.204 (talkcontribs)

Hi, I did this in Common.css:

<?php global $wgUser; if( !$wgUser->isAllowed('edit') ) { ?>

     <style type="text/css">
       #ca-watch { display: none !important; }
     </style> 
   <?php } ?>

But still see all tabs. What can be wrong? Thanks.

MarkAHershberger (talkcontribs)

Next time, please start a new thread.

Next, are you putting PHP code in your MediaWiki:Common.css page? That'll never work.

90.212.81.76 (talkcontribs)

Hi please could you upgrade from Mediawiki 16.5 to Mediawiki 1.19.8 please because Mediawiki 1.165 is now unsupported

SVG (talkcontribs)

Here it is for Vector skin. And don't remove my name, it's a real extension. Just create HideVariousTabsFromUnauthorizedUsers.php, put this code into it, include it in LocalSettings.php and if anonymous users DON'T have edit permission, it'll work. Actions at the arrow shouldn't need to be removed because I can't see an action shown under the arrow that can be executed by anonymous users with default or restricted permissions.

<?php
/**
* HideVariousTabsFromUnauthorizedUsers
*
* @package MediaWiki
* @subpackage Extensions
*
* @author: Tim 'SVG' Weyer <t.weyer@ymail.com>
*
* @copyright Copyright (C) 2012 Tim Weyer
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*
*/

$wgExtensionCredits['other'][] = array(
	'path'           => __FILE__,
	'name'           => 'HideVariousTabsFromUnauthorizedUsers',
	'author'         => array( 'Tim Weyer' ),
	'url'            => 'https://www.mediawiki.org/wiki/User:SVG',
	'description'    => 'Disables various view and namespace tabs from users without <tt>edit</tt> permission for Vector skin',
	'version'        => '04-12-2012',
);

// Hooks
$wgHooks['SkinTemplateNavigation'][] = 'fnHVTFUUremoveTabsFromVector';

// Tabs of view to remove
$wgHVTFUUviewsToRemove = array( 'view' /* read */, 'edit', 'addsection' /* on talkpages */, 'history' );

/**
 * @param $sktemplate Title
 * @param $links
 * @return bool
 */
function fnHVTFUUremoveTabsFromVector( SkinTemplate &$sktemplate, array &$links ) {
	global $wgUser, $wgHVTFUUviewsToRemove;

	// Only remove tabs if user isn't allowed to edit pages
	if ( $wgUser->isAllowed( 'edit' ) ) {
		return false;
	}

	// Generate XML IDs from namespace names
	$subjectId = $sktemplate->mTitle->getNamespaceKey( '' );

	// Determine if this is a talk page
	$isTalk = $sktemplate->mTitle->isTalkPage();

	// Remove talkpage tab
	if ( $subjectId == 'main' ) {
			$talkId = 'talk';
	} else {
			$talkId = "{$subjectId}_talk";
	}
	if ( !$isTalk && $links['namespaces'][$talkId] )
		unset( $links['namespaces'][$talkId] );

	// Remove actions tabs
	foreach ( $wgHVTFUUviewsToRemove as $view ) {
		if ( $links['views'][$view] )
			unset( $links['views'][$view] );
	}

	return true;
}
Holygamer (talkcontribs)

Works perfectly. Thanks. However the View Source tab still shows. Although you don't need permission to view that tab I would like to remove it as it's wasteful to Search Engine Optimization.

SVG (talkcontribs)

Sorry, forgot this tab because I didn't remove the edit permission when I was coding it. Just add 'viewsource' to $wgHVTFUUviewsToRemove array and it should work.

Jasper Deng (talkcontribs)

A long extension name at that :)

SVG (talkcontribs)

Yes, thought the same when I gave it its name. I just wanted to use a very meaningful name ^^

Bjoern~mediawikiwiki (talkcontribs)

Which version of mediawiki is this for?

I think I've followed the instructions above, but get an internal server error on

        // Generate XML IDs from namespace names
        $subjectId = $sktemplate->mTitle->getNamespaceKey( '' );
        // Determine if this is a talk page
        $isTalk = $sktemplate->mTitle->isTalkPage();

This post was posted by Bjoern~mediawikiwiki, but signed as Bjoern.

SVG (talkcontribs)

I've coded it in MediaWiki 1.17.0. Due to Holygamer's testimony, it does also work with MediaWiki 1.16.5 and I've checked both Vector.php. It might not work with MediaWiki 1.18, the code of Vector.php is quite different.

Cheeyang (talkcontribs)

For MediaWiki 1.19, try this:

function fnHVTFUUremoveTabsFromVector( SkinTemplate &$sktemplate, array &$links ) {
        global $wgUser, $wgHVTFUUviewsToRemove;

        // Only remove tabs if user isn't allowed to edit pages
        if ( $wgUser->isAllowed( 'edit' ) ) {
                return false;
        }

        // Remove talkpage tab
        if ( isset( $links['namespaces']['talk'] ))
                unset( $links['namespaces']['talk'] );

        // Remove actions tabs
        foreach ( $wgHVTFUUviewsToRemove as $view ) {
                if ( isset( $links['views'][$view] ))
                        unset( $links['views'][$view] );
        }

        return true;
}
125.21.230.132 (talkcontribs)
SVG (talkcontribs)

You just need to add 'viewsource' to $wgHVTFUUviewsToRemove array and it'll work. I don't get how is this related to ProtectSource extension. HideVariousTabsFromUnauthorizedUsers is NOT ProtectSource.

125.21.230.68 (talkcontribs)

HI Tim,

Could you please tell me in detail. Like do i need to update "LocalSettings.php" with below changes? $wgHVTFUUviewsToRemove['*']['viewsource'] = false; (or) do i need add like $wgHVTFUUviewsToRemove = array('viewsource');

I tried above given two option still i am able to see "View Source".

Thanks, Mohan

SVG (talkcontribs)

Hide viewsource tab for users without edit permission (usually unregistered users if you've set it so): $wgHVTFUUviewsToRemove = array( 'viewsource' );

125.21.230.132 (talkcontribs)

Hi Tim,

i want to hide "View Source" Tab for Anonymous users. In the LocalSettings.php If i set $wgGroupPermissions['*']['edit'] = false; then its hides the Edit Tab, Where as if i set $wgHVTFUUviewsToRemove = array( 'viewsource' ); its still shows the "View soruce" tab. So could you please help me. Thanks in advance.

Thanks, Mohan

This post was posted by 125.21.230.132, but signed as 125.21.230.68.

SVG (talkcontribs)

If you are not loggedin, you shouldn't be able to see viewsource tab (and no edit tab because edit tab gets into viewsource tab if you have no permission to edit pages). Do you use my code from above? You can try to purge the cache with ?action=purge

125.21.230.68 (talkcontribs)

Hi Tim,

i am not logged in but still it shows the "View source" tab. Can you tell me which code & in which file i have to update your code? bcoz my mediawiki version is -1.18.3. The tell me how to try Purge option (under in which file). Rgds, Mohan

SVG (talkcontribs)

Create folder HideVariousTabsFromUnauthorizedUsers in extensions directory, create HideVariousTabsFromUnauthorizedUsers.php file in HideVariousTabsFromUnauthorizedUsers folder and add the code below to your this file. Then add require_once ( "$IP/extensions/HideVariousTabsFromUnauthorizedUsers/HideVariousTabsFromUnauthorizedUsers.php" ); to your LocalSettings.php. Remove $wgGroupPermissions['*']['edit'] = false; and any other earlier setting of HideVariousTabsFromUnauthorizedUsers extension from LocalSettings.php.

<?php
/**
* HideVariousTabsFromUnauthorizedUsers
*
* @package MediaWiki
* @subpackage Extensions
*
* @author: Tim 'SVG' Weyer <t.weyer@ymail.com>
*
* @copyright Copyright (C) 2012 Tim Weyer
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*
*/

$wgExtensionCredits['other'][] = array(
	'path'           => __FILE__,
	'name'           => 'HideVariousTabsFromUnauthorizedUsers',
	'author'         => array( 'Tim Weyer' ),
	'url'            => 'https://www.mediawiki.org/wiki/User:SVG',
	'description'    => 'Disables various view tabs (and originally also namespace tabs) from users without <tt>edit</tt> permission for Vector skin',
	'version'        => '05-28-2012',
);

// Hooks
$wgHooks['SkinTemplateNavigation'][] = 'fnHVTFUUremoveTabsFromVector';

// Tabs of view to remove
$wgHVTFUUviewsToRemove = array( 'viewsource' );

// Remove 'edit' permission from anonymous users
$wgGroupPermissions['*']['edit'] = false;

/**
 * @param $sktemplate Title
 * @param $links
 * @return bool
 */
function fnHVTFUUremoveTabsFromVector( SkinTemplate &$sktemplate, array &$links ) {
	global $wgUser, $wgHVTFUUviewsToRemove;

	// Only remove tabs if user isn't allowed to edit pages
	if ( $wgUser->isAllowed( 'edit' ) ) {
		return false;
	}

	// Remove actions tabs
	foreach ( $wgHVTFUUviewsToRemove as $view ) {
		if ( $links['views'][$view] )
			unset( $links['views'][$view] );
	}

	return true;
}
Brocchinia (talkcontribs)

Hi Tim

It seems to work so far for me, THANKS for this extension. I altered this

// Tabs of view to remove

$wgHVTFUUviewsToRemove = array( 'edit', 'history', 'read', 'talk', 'viewsource' );

But it looks like it only works in the main namespace. Tabs of Pages in the MediaWiki or User Namespace still appear. Any ideas ? If possible I would like to hide all Tabs for unlogged users

SVG (talkcontribs)

What version of MediaWiki do you use and how are the settings of your wiki's user rights? Sorry for my late reply!

Tickihgk (talkcontribs)

Hi Tim,

is your extension still working with version 1.19.2. Maybe because of some different code in the newest vector skin? Thanks a lot!

122.176.133.214 (talkcontribs)

is there anyway i can remove the page title tab also? Eg: the tab named "Article" if u open Wikipedia. So i want to hide that tab too. Please help

MarkAHershberger (talkcontribs)

Untested: if you add "main" to that list it might work.

Coffeehound (talkcontribs)
85.240.32.6 (talkcontribs)

This works like a charm!

It hidden history and viewsource as i wanted. did not work with talk but the other two were the main important. Talk editing will be denied (anonymous edit off) so what I needed is done with this.

Thanks a lot!!

David

Iantresman (talkcontribs)

Could HideVariousTabsFromUnauthorizedUsers be extended to prevent anonymous non-editors from viewing revisions, ie. any link with an &oldid, as seen on the Revision history page under the links for "cur" and "last" and the revision date?

125.21.230.132 (talkcontribs)

Hi Tim,

i want to hide "View Source" Tab for Anonymous users. In the LocalSettings.php If i set $wgGroupPermissions['*']['edit'] = false; then its hides the Edit Tab, Where as if i set $wgHVTFUUviewsToRemove = array( 'viewsource' ); its still shows the "View soruce" tab. So could you please help me. Thanks in advance.

Here my requirement is that Anonymous users should not see "Edit & View Source" tabs.

Thanks, Mohan

Duplicate striked by SVG.

Besma~mediawikiwiki (talkcontribs)

Hi, Thank you Tim! This extension is pretty cool but it works only with the vector skin! I tried these few lines in the Mediawiki:Common.css and it works :

  1. ca-viewsource { display: none !important; }
  2. ca-talk { display: none !important; }

just put the right tab's id !!

This post was posted by Besma~mediawikiwiki, but signed as Besma.

Cheeyang (talkcontribs)
132.209.111.2 (talkcontribs)

This worked for me, on the Vector Theme:

protected function renderPortals( $portals ) {
    '''global $wgUser;'''

then:

case 'TOOLBOX':
    '''if($wgUser->isAllowed( 'edit' )) {'''
        $this->renderPortal('tb', $this->getToolbox(), 'toolbox', 'SkinTemplateToolboxEnd');
    '''}'''
    break;
90.212.81.76 (talkcontribs)

Hi please could you upgrade from Mediawiki 16.5 to Mediawiki 1.19.8 please because Mediawiki 1.165 is now unsupported

SVG (talkcontribs)

Hi. I haven't committed to MediaWiki for a long time. But if you really need this, contact me right here.

88.130.123.87 (talkcontribs)

Tim, this topic pops up here again and again. There surely is a need for such a feature. If you could add this, it will definitely make many peoples' lives easier.

Gleki.arxokuna (talkcontribs)

This doesn't work on Mediawiki 1.23. Namely, it hides all toolboxes but doesn't hide "Edit", "edit source" and "History" tabs. And it does hide "Tools" toolbox which I don't want to hide. Any solutions?

MarkAHershberger (talkcontribs)

MW 1.23 hasn't been released yet. This sounds like a bug. Could you report it?

202.89.105.45 (talkcontribs)

How do i hide Discussions tab & left sided navigation tabs for unlogged users.please send your reply to mail id satish_nunesh@yahoo.com