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

Nathanael81’s picture

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!

asymptotixweb’s picture

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:

$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.

Randall Shelley’s picture

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.

  if(($is_front) && ($logged_in)) { 
    header ("Location: /yourdesiredpath"); 
  }
asymptotixweb’s picture

Here is an update of the code:

$node = menu_get_object();
 
// deeplink in a mail to the intranet (limited to authenticated users)
if (arg(0) == 'user' && arg(1) == 'login' ) {
 $query = $_GET;
    unset($query['q']);
    drupal_goto('user/login', array(
      'destination' => url('oauth/authorize', array(
        'query' => $query,
      )),
    ));
}
 
// The statement below concerns a view of a map, ie it does not have a node id.
elseif (arg(0) == 'about' && arg(1) == 'branches' && arg(2) == NULL) {
  return 'my';
}
 
// 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 'my';
}
 
// The statement below concerns another view, path is /success-stories. It does not have a node id.
elseif (arg(0) == 'success-stories') {
return 'my';
}
 
// The statement below redirects to the intranet homepage (/my) if the login take place at the homepage (which in this case is node/739)
elseif ($node->nid == 739) {
  return 'my';
}
 
// this redirect to destination after successful login on /access-denied page
elseif (strpos($_SERVER['REQUEST_URI'], "/access-denied")===0) {
  return  $_GET['destination'];
}
 
// The last statement: if none of the first three conditions are valid then do not redirect!
else {
  return 'node' . "/" . $node->nid;
}
System Lord’s picture

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?