Drupal 7 /user url goes to a page that has 2 tabs Login and Request new password which is what I want for all of our users except for one. How can I NOT show the Request new password tab within the /user (login) page for one particular user ($user->name)?
2 Answers
If you just wish to hide the tab, you can do that by using the Tab Tamer module. However, the user would still be able to access the form through the URL http://<yoursite>/user/password
.
If you wish to disable (along with hiding) the password tab, you can implement the hook_menu_alter().
function MODULE_menu_alter(&$items) {
//condition to check the user
global $user;
if($user->id == '<USER_ID_OF_THE_USER>') { //can use $user->name as well
$items['user/password']['access callback'] = FALSE;
}
}
I found this looking for solutions to the same problem. However, this code will not work because hook_menu_alter and hook_menu only run when the menu's are rebuilt. So, the if clause will only execute when the user, most likely admin, does a cache clear, not when the targeted user is using the site.
My solution:
function MODULE_menu_alter(&$items) {
$items['user/password']['access callback'] = 'MODULE_user_access_callback';
$items['user/%user/edit']['access callback'] = 'MODULE_user_access_callback';
}
function MODULE_user_access_callback() {
global $user;
// target "guest" account or "anonymous"
if ($user->name == 'guest' || $user->uid == 0) {
return FALSE;
} else {
return TRUE;
}