4

I'm writing a module that redirects the user on login and am having trouble getting anything to work. Here is what I tried within hook_user_login:

submission.info

name = Submission
description = Handles Redirect to node/add/page on Login 
core = 7.x

submission.module

function submission_user_login(&$edit, $account) {
  //$_REQUEST['destination'] = 'node/add/page';
  //$GLOBALS['destination'] = 'node/add/page';
  //$edit['redirect'] = 'node/add/page';
  drupal_goto('node/add/page');
}

3 Answers 3

7

Try to change drupal_goto with $_GET['destination'], e.g.:

function submission_user_login(&$edit, $account) {

   // Redirect user to to desired location.
   $_GET['destination'] = 'node/add/page';
}

Hope this will work.

2
  • this works. but i had to remove the two single quotes.
    – Emanegux
    Mar 19, 2014 at 6:09
  • @Rupesh : Thanks to edit the answer, it was typo I guess. Emanegux glad that could help you.
    – CodeSurfer
    Mar 19, 2014 at 6:50
1

You can use Login Destination module to redirect user after login. This module have many customization for redirecton of user. For more info go through the documentation of module.

1
  • As you can see in the question, I've already tried this. Please edit the question if this is not clear.
    – Emanegux
    Mar 19, 2014 at 6:03
0

You can use Rules module to do this very easily..

The Rules module allows site administrators to define conditionally executed actions based on occurring events (known as reactive or ECA rules).

OR

You can use below code to redirect inside hook_user_login implementation ..

function YOURMODULENAME_user_login(&$edit, $account)  {
  $GLOBALS['destination'] = 'node/add/page';
}

You also need to implement hook_drupal_goto_alter to perform redirect

/**
 * Implements hook_drupal_goto_alter
 */
function YOURMODULENAME_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  // Note that this functionality cannot be backported do 6.x as Drupal 6 does
  // not call drupal_alter for drupal_goto.

  // This actually may be used also by templates.
  if (isset($GLOBALS['destination'])) {
    $destination = $GLOBALS['destination'];

    // alter drupal_goto
    if (is_array($destination)) {
      $path = $destination[0];
      $options = array();
      if (count($destination) > 1) {
        $options = $destination[1];
      }
    }
    else {
      $path = $destination;
    }
  }
}
8
  • drupal_goto also does not work, even with a fresh d7 installation, and I do not want to rely on admin site configuration through contributed modules until I have exhausted all my options within the core APIs.
    – Emanegux
    Mar 19, 2014 at 4:51
  • Did you clear your cache after code implementation ? Can you make sure your hook is firing using drupal_set_message("Hello"); inside your function ?
    – Anil Sagar
    Mar 19, 2014 at 5:03
  • Yes, my cache was cleared after implementing drupal_goto('node/add/page'); and it still does not work.
    – Emanegux
    Mar 19, 2014 at 5:06
  • Do you see drupal_set_message("Hello"); getting triggered and message "Hello" has been displayed in screen when logged in ? Is your custom module enabled ?
    – Anil Sagar
    Mar 19, 2014 at 5:08
  • yes, I have tested the hook before posting this question. the issue this concerns is redirect not working within a custom module on fresh install.
    – Emanegux
    Mar 19, 2014 at 5:12

Your Answer

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

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