Remove unwanted tabs from pages

Both Drupal core as well as various modules add tabs to pages that are not needed for general users, or not needed at all. You may wish to link to the page in a different way, as some users don't understand that they can click on the tab above a node.

Method for Drupal 4.7.x and Drupal 5

In versions of Drupal prior to 6, there was not a way to alter the hook_menu() generated tabs from code, so the function illustrated on this page will find and strip out a tab based on its name. As of Drupal 6, there is now the hook_menu_alter() function, which allows you to alter various properties of menu items that are set by core or other modules. If you are using Drupal 6, you should consider using hook_menu_alter instead, which is more exact, and is simpler/more efficient. One thing that hook_menu_alter cannot do that this page's method can is remove the "View" tab (if you have reason to remove the View tab, you can do so with this technique, and use hook_menu_alter for the rest of your tab removals).

Step 1 of 2

Locate your theme's template.php file. If one doesn't exist, create an empty one. This is where you can place customization PHP code.

Step 2 of 2

Custom functions placed in the themes template.php file should begin with the theme name. In the code snippet below replace "yourthemename" with the actual name of your theme, such as "garland".

You may already have a '_phptemplate_variables' function defined depending on what theme you are using, if so do not include the function again from the snippet below.

<?php
function _phptemplate_variables($hook, $vars = array()) {

  if (
$hook == 'page') {
   
yourthemename_removetab('address book', $vars);
   
// add additional lines here to remove other tabs.
 
}

  return
$vars;
}

function
yourthemename_removetab($label, &$vars) {
 
$tabs = explode("\n", $vars['tabs']);
 
$vars['tabs'] = '';

  foreach (
$tabs as $tab) {
    if (
strpos($tab, '>' . $label . '<') === FALSE) {
     
$vars['tabs'] .= $tab . "\n";
    }
  }
}
?>

The tab removal work is done in the yourthemename_removetab() function, pass in a plain text tab label, along with the PHPTemplate variables, and the function will remove the tab.

In the above example snippet the 'address book' tab added by the eCommerce package is removed from the user's profile page.

Notes

  • Call yourthemename_removetab('tab name', $vars); for each tab you wish to remove.
  • No other modules need to be installed to use this.
  • If you need more exact control of which page on the site to affect (since different tabs might share the same name), you can wrap the removetab function with conditions that check the URL path arguments.

Method for Drupal 6

As mentioned in the introduction, generally you should consider using Drupal 6's hook_menu_alter to remove tabs instead. However if you need to use this page's tab removal method, the below code is adapted for use in Drupal 6. The notes and info in the above steps still apply in the same way.

First either add the first code snippet as shown, or else merge it with your current preprocess_page() function in template.php (you cannot have two of the same function, so look for preprocess_page). In both code snippets, change all instances of "yourthemename" to the name of your current theme, and do not include the opening and closing PHP tags.

<?php
function yourthemename_preprocess_page(&$vars) {
 
// Remove undesired local task tabs.
  // This first example removes the Users tab from the Search page.
 
yourthemename_removetab('Users', $vars);
}
?>

Then add this function "outside" of the yourthemename_preprocess_page() function:

<?php
// Remove undesired local task tabs.
// Related to yourthemename_removetab() in yourthemename_preprocess_page().
function yourthemename_removetab($label, &$vars) {
 
$tabs = explode("\n", $vars['tabs']);
 
$vars['tabs'] = '';

  foreach (
$tabs as $tab) {
    if (
strpos($tab, '>' . $label . '<') === FALSE) {
     
$vars['tabs'] .= $tab . "\n";
    }
  }
}
?>

After saving template.php, you may need to rebuild the theme registry before your changes will take effect. This can be done by clearing the cache at Administer > Site configuration > Performance, or through helper modules you likely have such as Admin Menu (top left icon) or Devel (in the Devel block).

A slightly cleaner way

alex.k@drupal.org - February 11, 2008 - 15:26

This method works for me and is cleaner (IMO) and more performant than the regexp method:
Tabs are the default way of rendering menu items of type MENU_LOCAL_TASK or MENU_DEFAULT_LOCAL_TASK. So, there is a theming function theme_menu_local_task() in menu.inc that renders them. If you override it in your theme, you can skip certain tabs by simply returning the empty string as their rendered html:
This function would go into template.php in your theme.

<?php
function phptemplate_menu_local_task($mid, $active, $primary) {
 
//Check which tab is being rendered
 
$item = menu_get_item($mid);
 
//Remove core search tab
 
if ($item['path'] == 'search/node') {
    return
'';
  }
 
//The rest is copied from theme_menu_local_task()
 
if ($active) {
    return
'<li class="active">'. menu_item_link($mid) ."</li>\n";
  }
  else {
    return
'<li>'. menu_item_link($mid) ."</li>\n";
  }
}
?>

This example removes the core search tab.

This does not work with some paths

stevekerouac - February 13, 2008 - 13:27

For example,

if ($item['path'] == 'user') {
    return '';
  }

does not remove the Log In tab.

user/login

alex.k@drupal.org - February 18, 2008 - 16:08

The path to the login tab is 'user/login' for me (4.7). If you use that it should work. It's the menu item's path, not the path of the request that should be checked.

Yes, thanks

stevekerouac - April 2, 2008 - 09:13

That did it in 5.7 too.

Customized

eXce - May 24, 2008 - 11:22

Customized it to avoid the Tabs on search-subsites (if you search for "dog" the tab path is "/search/node/dog" and it doesn't work.
I avoided regexp and used str_replace due perfomance reasons.

<?php
function phptemplate_menu_local_task($mid, $active, $primary) {
 
//Array with Tabs to be removed
 
$disabled_tabs = array('search/user' , 'search/node');


 
//Check which tab is being rendered
 
$item = menu_get_item($mid);
   
  foreach (
$disabled_tabs as $tab) {
      if (
substr($item['path'],0,strlen($tab))==$tab) {
          return
'';
      }
  }
 
 
//The rest is copied from theme_menu_local_task()
 
if ($active) {
    return
'<li class="active">'. menu_item_link($mid) ."</li>\n";
  }
  else {
    return
'<li>'. menu_item_link($mid) ."</li>\n";
  }
}
?>

My task was to combine

urix - September 12, 2008 - 09:31

My task was to combine profilesearch module with default search module.
Here is the code. I keep tab title, but redirect user to another url (profilesearch/search_string).
Drupal 5.10.

<?php
function phptemplate_menu_local_task($mid, $active, $primary) {
 
//Check which tab is being rendered
 
$item = menu_get_item($mid);
 
//Remove core search tab
 
if (stristr ($item['path'], 'search/user')) {
   
$search_string=substr($item['path'], strpos($item['path'],'search/user/')+12 );
    return
'<li><a href="/profilesearch/'.$search_string.'">'.$item['title']."</a></li>\n";
  }
 
//The rest is copied from theme_menu_local_task()
 
if ($active) {
    return
'<li class="active">'. menu_item_link($mid) ."</li>\n";
  }
  else {
    return
'<li>'. menu_item_link($mid) ."</li>\n";
  }
}
?>

Drupal 6.x

madsph - January 29, 2009 - 13:48

I had a similar problem with drupal 6, and I couldn't get your fix to work.

What I did in stead was to add a new submit function to the search forms which makes a redirect to my custom search module (called 'userfinder') - heavily inspired by how the search module redirects to search/node.

This way the results of my own search module is presented first to the user. At first I wanted to remove the 'Content' and 'User' search tabs as well, and followed vijaythummar suggestion which worked perfectly. I ended up deciding to keep them as 'back-up' facilities though.

So in my userfinder.module I have:

/**
  * Implementation of hook_form_alter().
  */
   function userfinder_form_alter(&$form, &$form_state, $form_id) {
     //This code gets called for every form Drupal build; so be sure to bail out quick
     // if the desired form is not present
     if ($form_id == 'search_block_form' || $form_id == 'search_form') {
       //Redirect to our own usersearch
       $form['#submit'][] = 'userfinder_search_submit';
     }
   }

   function userfinder_search_submit($form, &$form_state) {
     $form_id = $form['form_id']['#value'];
     $args = trim($form_state['values'][$form_id]);
     if ($args && $args != '') {
       $form_state['redirect'] = 'search/userfinder/'. $args;
     }
   }

Doesn't work on 'Content' tab of Search page

gbhatnag - March 25, 2008 - 21:58

For some reason, if I try and remove the 'Content' tab on the Search page, the styling of the tabs gets messed up (there are no more tabs, just a real plain looking bulleted list!). I haven't investigated the code much yet, but looks like there is something special about the first tab in a series of tabs (other tabs that aren't first seem to get cut just fine).

User tab

xurizaemon - June 11, 2008 - 03:55

Please note that the simple method for removing the User Search functionality is to disable the permission "access user profiles" for the role which you don't want seeing it. You may only be seeing this because you're logged in as an administrator.

A version of this for 6.x

gbear - September 10, 2008 - 17:13

I needed to remove tabs in search & that IMCE file management tab in user accounts. Used strpos in php5, sounds like its fastest.
This would go in template.php still.

<?php
/*
* Remove search users tab & the file upload tab for all users.
*/
function phptemplate_menu_local_task($link, $active = FALSE) {
  if (
strpos($link,'search') ) {
    return
'';
  }    else if (
strpos($link,'imce') ) {
    return
'';
  } else {
      return
'<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
  }
}
?>

Thanks for that! I was

harrisben - November 7, 2008 - 04:08

Thanks for that!

I was trying to remove the 'Request new password' tab on the user login form and for some reason it wouldn't disappear until I realise that I needed to match the case of the text (capital R). Tada! It works!

Remove unwanted tabs from pages in Drupal6

vijaythummar - November 21, 2008 - 09:57

Here what I have used to remove unnecessary tabs in drupal6.

<?php
function phptemplate_preprocess(&$variables, $hook) {
  if(
$hook == 'page') {
   
yourthemename_removetab('File browser', $variables);
  }
  return
$variables;
}

function
yourthemename_removetab($label, &$vars) {
 
$tabs = explode("\n", $vars['tabs']);
 
$vars['tabs'] = '';

  foreach(
$tabs as $tab) {
    if(
strpos($tab, '>' . $label . '<') === FALSE) {
     
$vars['tabs'] .= $tab . "\n";
    }
  }
}
?>

Regards,
vijay thummar

Thank you! I've been looking

wxman - January 4, 2009 - 15:51

Thank you! I've been looking everywhere for a way to do this.

Nice!

feconroses - January 26, 2009 - 19:15

That actually works! Was looking for ages for a solution like that. Thanks :)

Could you post some code to:

- Edit existing tabs.
- Only delete/edit tabs from certain pages and not from all pages.

I would be really grateful! Thanks in advance!

only in certain pages

pbattino - March 27, 2009 - 11:44

if you want this to hide tabs only in certain pages you might want to check the URL via the arg() function. For example, to hide some tabs only in the user profile pages:

replace

<?php
if($hook == 'page')
?>

with

<?php
if($hook == 'page' && arg(0)=='user')
?>

Another way to do this...

keystr0k - June 11, 2009 - 03:10

I am not sure if this is very efficient, or foolproof, but it's working for me so far:

<?php
/**
* If the user isn't an admin, or the original administrative user,
* remove the "edit" tab for the "note" nodes in the my_garland theme.
*/
function phptemplate_preprocess(&$variables, $hook) {
    global
$user;
    if(
$hook == 'page' && !in_array('admin',array_values($user->roles)) && $user->uid != 1){
       
$nid = str_replace('page-node-','',$variables['template_files'][1]);
       
$node = node_load($nid);
        if(
$node->type == 'note'){
           
my_garland_removetab('Edit', $variables);
        }
    }
    return
$variables;
}
?>

I presume we have to change

Ariesto - February 22, 2009 - 05:10

I presume we have to change 'page' to a different content type if it applies. I changed youthemename to my theme name. With those changes (and removing the php tags) I could not get the "View" tab to go away..

No it has nothing to do with

Keyz - February 22, 2009 - 09:20

No it has nothing to do with the Page content type. Page here refers to page.tpl.php
You can read more about it here http://drupal.org/node/223430

could'nt remove "Log in" tab

shriya_atreya - April 14, 2009 - 07:22

could not remove "Log in" tab in Drupal 6 using any method specified here

this helped me

Ariesto - February 22, 2009 - 17:08

This forum post helped me solve my tabs problem. I think that the example above works as well, I just didn't realize that I had 2 preprocess functions!

http://drupal.org/node/379792

name for backlinks tab

elallaix - March 8, 2009 - 15:56

I would like to remove backlinks tab but all I tried doesn't function.
Can you help me?
Is it correct "backlinks" name to remove backlinks tab?

i think views does this

Ariesto - April 16, 2009 - 22:29

Check your view. I believe it has an option to turn off the backlinks tab.

only in certain pages

pbattino - March 27, 2009 - 11:42

if you want this to hide tabs only in certain pages you might want to check the URL via the arg() function. For example, to hide some tabs only in the user profile pages:

replace

<?php
if($hook == 'page')
?>

with

<?php
if($hook == 'page' && arg(0)=='user')
?>

remove some tabs only for certain types of users

sridharraov - May 25, 2009 - 19:13

I have different types of users. So i have different categories in profile. Now based on the user type I need to display certain tabs. Is there a easy way to do it or that i need to write a query to extract value from profile_values table and add the appropriate conditions in the proprocess funtion...

Removing a tab for a specific content type and user roles

keystr0k - June 11, 2009 - 03:12

Sorry to post again...
I am not sure if this is very efficient, or foolproof, but it's working for me so far:

<?php
/**
* If the user isn't an admin, or the original administrative user,
* remove the "Edit" tab for the "note" nodes in the my_garland theme.
*/
function phptemplate_preprocess(&$variables, $hook) {
    global
$user;
    if(
$hook == 'page' && !in_array('admin',array_values($user->roles)) && $user->uid != 1){
       
$nid = str_replace('page-node-','',$variables['template_files'][1]);
       
$node = node_load($nid);
        if(
$node->type == 'note'){
           
my_garland_removetab('Edit', $variables);
        }
    }
    return
$variables;
}
?>

That works fine, and so does

Netbuddy - June 23, 2009 - 16:43

That works fine, and so does the method for Drupal 6 using menu_alter. The problem im having is I cant disable the functionality, only the tab.

For example. I have Node Gallery installed, I created an "Upload Images" tab, but of course it appears on all standard pages using MENU_LOCAL_TASK, not just the gallery pages. I can remove it easily enough using the above method for node type blog, page, story etc...but the user can still type in the direct URL and get the functionality - http://www.mydomain.com/node/22/upload...is a book node, without blocking actual functionality a user can upload images into book nodes, rather than the gallery itself!!! eeek.

I either need a way to make sure this tab only works with certain modules - in this case the node gallery module, or i need to block direct url access to the upload page on every other part of the site apart from the gallery...

Permissions group?

mudd - July 9, 2009 - 15:37

It would be nice if the "user module" permissions group had an 'edit own account' checkbox, and have any module that wants to put a tab under My Account be required to put their own checkboxes here.

To be practical (since that suggestion isn't trivial), and since my site just disallows users to change email address or password, perhaps give us a way to hide certain widgets ("Account information" in my case).

remove all tabs from everywhere, for all users on drupal 6.x

kinshuksunil - July 17, 2009 - 08:00

if you wanna remove each an devery tab from everywhere in the website, for every user (which, i needed for a weird reason) do this:

<?php
function yourthemename_preprocess_page(&$vars) {
 
// Remove all local task tabs, from everywhere on the site, for all users.
$vars['tabs'] = "";
}
?>

(o_0)
confused yet acting smartest of alll....

 
 

Drupal is a registered trademark of Dries Buytaert.