Login Destination Snippets
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
PHP Snippets Library:
(Don't put PHP tags when creating snippets. )
Here's a snippet for redirecting to any page you want that Views returns. For instance, if you have March, April, and May newsletter drafts posted in March, April, and May, respectively, and you want to redirect your editors on login to the May newsletter draft -- then create a view called 'draft', filtering for newsletters, sorting descending by posted date so that the May newsletter is listed first, and returning a Node: Nid field, and enter the following code in your Login Destination settings:
global $user;
// Redirect the Administrator
if ($user->uid == 1) {
return 'admin';
}
// Redirect the Editor role
elseif ($user->roles[2]) {
// Load a view by name
$view = views_get_view('draft');
// Get an array of view result objects
$view->render();
return ('node/' . $view->result[0]->nid);
}
// Redirect everyone else
else {
return 'node';
}
$view->result[0]->nid returns the nid of the first newsletter in the results, which is May because you sorted descending by posted date.
PHP snippet for redirection URL should return a string. Here is an example:
global $user;
if ($user->uid == 1) {
// Redirect the Administrator
return 'admin';
} elseif ($user->uid == 2) {
// Redirect the Site Owner to the 'create content' page
return 'node/add';
} else {
return 'node';
}
PHP snippet for Redirection condition should return boolean value. An example is:
return ($_GET['q'] == 'user/login');
Various other snippets
// --- role based
global $user;
if (in_array('role1', $user->roles)) {
return 'node1';
}
if (in_array('role2', $user->roles)) {
return 'node2';
}else {
return '<front>';
}
//-- example with &&
if ( in_array('role1', $user->roles) && in_array('role2', $user->roles) ) {
return 'node3';
}
//---based on role once more
global $user;
if (in_array('role1', $user->roles)) {
// Redirect users will role1 role
return 'role1-url';
} elseif (in_array('role2', $user->roles)) {
// Redirect users will role2 role
return 'role2-url';
} else {
return 'default-url';
}
//--- based on the "first" OG group
//--- (we had a site where members were only in one group)
if ($group = current($user->og_groups)) {
return 'node/' . $group['nid'];
// if you have a path alias for that node, (groups/Maryland, for example),
// the alias will show up as the actual URL for the user
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion