I want to redirect a user when they login based on what role they have and whether they have created a node of a type.

So if the user has administrator role, direct user to admin url else if the user has role list_user, check if user has content of type new listing, if they don't, direct them to create node listing if they do have created node of type listing, direct them to profile.

I have this set up in separate rules but I'm sure it will work better and faster by just having one php block of code in the template.php

My only problem is, I don't know php and everything I've tried doesn't work very well or not at all.

Any help would be appreciated.

3 Answers 3


Use Rules module to redirect based on user roles.

  1. After Installing Rules module, Go to Admin -> Configuration -> Rules (admin/config/workflow/rules/)
  2. Add new rule
  3. Enter rule name & select User has logged in from React on Event
  4. Add new condition and set User has Role Select the required role and Save
  5. In Action add new action Set it Page Redirect & enter the redirection URL

You should create basic module instead of heaving it in your theme (template.php file).

Create empty directory called your_module in sites/all/modules and create your_module.info and your_module.module files inside. Here is info what should you put into .info file. For basic implementation you need only name and core properties.

.module file must implement hook_user_login() (this hook will be triggered after user login). Read more here. Here is pseudocode implementation of your problem:

function your_module_user_login(&$edit, $account) {
  if (something) {
    $edit['redirect'] = 'admin';
  elseif (something_else) {
    $edit['redirect'] = 'admin/content';
  elseif (something_else_2) {

Read more how to create drupal module


This is a fairly vague questions as few different problems need to be solved before you have a complete solution. I think @zaporylie is right and is a push in the right direction. My answer here acts as further help; but you will need to find out how to get details of the node type and what content types the user has. I use a debugger PHPStorm; that when you create a breakpoint you can all the variables that are available to retrieve.

If you want to find a user role you can use ( the elseif is a guess ! )

global $user;

if (in_array('administrator', $user->roles) {
elseif (in_array('list_user', $user->roles) && $node->type == $user->contenttype)){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.