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
}
Comments
Can someone explain please
I want users with a given role be redirected to a certain node, I didn't really get into the PHP coding yet, so I'm not sure where and how to type in the values (Roles/Nodes) of my page...
if I used this code
would I just replace 'role1' with 'my role' and 'node1' with my 'node22' (without slash?)???
which code to put into Redirection conditions and which into Destination URL settings?
Another question is, if I have spaces in my roles, does it work or how do I code them?
Thanks in advance!
Login redirection depending on current node's nid
My client wanted users to be redirected to various places depending on the current node of the anonymous visitor.
The homepage was node/739 and if the visitor was there when logging in he/she would be redirected to the intranet's homepage: /my. Otherwise it should not redirect. The problem is that two pages were rendered by views, so I had to add more conditions.
Here is my code:
Hope you find this useful.
Simple Redirect Drupal 7 Solution
This is how i did it. Put this at the top of your Drupal 7 page.tpl
When logged in and this is the front page.
Here is an update of the
Here is an update of the code:
Login destination to a term
Hi, I have about 30,000 city taxonomy terms. My users select their state and city during profile setup (i.e., term reference via fields). I would like to have my users land on their "city term" whenever they login. I'm using project/login_destinations. It allows PHP. Would anyone know what the code would be to make this happen?