Community Documentation

Login Destination Snippets

Last updated November 3, 2010. Created by sapark on March 30, 2009.
Edited by eporama, setfree. Log in to edit this page.

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:

<?php

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:

<?php
 
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:

<?php
 
return ($_GET['q'] == 'user/login');
?>

Various other snippets

<?php
// --- 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

<?php
// --- role based

global $user;
if (
in_array('role1', $user->roles)) {
    return
'node1';
}
if (
in_array('role2', $user->roles)) {
    return
'node2';
}else {
    return
'<front>';
}
?>

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:

<?php
$node
= menu_get_object();
// The statement below concerns a view of a map, ie it does not have a node id.
if (arg(0) == 'about' && arg(1) == 'branches' && arg(2) == NULL) {
return
arg(0) . "/" . arg(1);
}
// The statement below concerns a view of a city (arg(2) is the country in the path and arg(3) is the city) in a map, ie it does not have a node id.
elseif (arg(0) == 'about' && arg(1) == 'branches') {
return
arg(0) . "/" . arg(1) . "/" . arg(2) . "/" . arg(3);
}
// The statement below concerns another view which path is /success-stories. It does not have a node id.
elseif (arg(0) == 'success-stories') {
return
arg(0) . "/" . arg(1);
}
// The statement below redirects to the intranet homepage (/my) if the login takes place at the homepage (which in this case is node/739)
elseif ($node->nid == 739) {
return
'my';
}
// The last statement: if none of the first three conditions are valid then do not redirect!
else {
return
'node' . "/" . $node->nid;
}
?>

Hope you find this useful.

Peter Lindmark