I want to redirect users to a custom page, after they create a node or edit it.

up vote 6 down vote accepted

You can use hook_form_alter to add a custom submit handler where you can set the redirection path.

See the FAPI for more details about form redirection.

  • 1
    Thanks for your answer ! i've read about the FAPI but a still have question to go ahead : Where should i put the hook_form_alter function ? – ousneo May 9 '11 at 10:24
  • 2
    @ousneo In drupal 7 you can put it in your theme or in a custom module you create. – googletorp May 9 '11 at 10:49
  • 1
    please see @Dmitry Vyal answer – Capi Etheriel Jan 11 '12 at 14:57
  • 1
    Here's a module that does a redirect after saving. drupal.org/project/node_save_redirect – polyclick Feb 18 '14 at 10:32
  • 1
    While accepted, this answer is too brief to be useful. It really matters how you add your custom submit handler, and this answer is silent on that. Best answer IMHO is that from @leymannx (but see by comment about the edit use case). – Free Radical Aug 16 at 10:30

You can create a rule that redirects the user to any page when they create or edit a specific content type. When you add a rule select "After Creating New Content" add also a condition of "Content Has Type". You Do section will give you the option to redirect a user to any page you want.

For a redirect after editing a node, just create a new rule and select "After updating Existing Content" as your first option.

  • I used Rules to redirect after creating a specific content, but the user gets no feedback now. I want to show the user that "Your content is created". How can I improve this ? – wallace740 Apr 24 '12 at 14:59
  • 1
    Add another action after the redirect action to "Show a configurable message on the site" and add your custom message from there. – landing Apr 26 '12 at 17:05
  • I added another Rule but i think I miss something in my custom theme because the message is not shown on my frontpage. I'm using a module 'Better messages' to show these kind of messages in a popup !rule config – wallace740 Apr 27 '12 at 9:24
  • My experience is that overlay breaks Rules redirecting, beware. – Capi Etheriel Jul 3 '12 at 23:29
  • @landing 's solution worked great for me. Thanks! – hockey2112 Apr 24 '13 at 22:02

When doing this, ensure to also unset a possible existing destination which normally is added to the URL when you click the edit button to edit an existing node.

Replace article in MYMODULE_form_article_node_form_alter with your content type's machine name.

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function MYMODULE_form_article_node_form_alter(&$form, &$form_state, $form_id) {

  $form['actions']['submit']['#submit'][] = '_MYMODULE_node_form_redirect';
}

/**
 * Submit callback.
 */
function _MYMODULE_node_form_redirect($form, &$form_state) {

  // Unset destination first.
  unset($_GET['destination']);
  unset($_REQUEST['edit']['destination']);

  $form_state['redirect'] = 'FOO/BAR';
}

The documentation is not very clear on it, but you should modify $_GET['destination'] variable, not $_REQUEST or $form_state['redirect'].

  • 1
    Modifying $form_state['redirect'] works fine in Drupal 7... – Felix Eve Feb 11 '14 at 9:49

in Drupal 7 you can alter the node edit form to 'attach' your own submit function, in which you can alter form_state of the node delete confirmation form to include the 'destination' parameter... just did this in a custom theme i've been developing as a subtheme of the 'seven' admin theme...

Documentation for hook_form_FORM_ID_alter

You can add this hook to a template/theme, not just to modules.

so in your theme's template.php, add this form alter hook:

function yourtheme_node_delete_form_submit( &$form, &$form_state ) {
    $form_state[ 'redirect' ][ 1 ][ 'query' ][ 'destination' ] = "admin/content";
}

and then, voila. no longer redirected to homepage after a node is deleted.

Solution from here http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#comment-18139

<?php
  function FORM_NAME_form_submit($form, &$form_state) {
    $form_state['redirect'] = array(
      // $path
      'path/to/redirect/to',
      // $options
      array('query' => array('key' => 'value')),
      // $http_response_code
      302,
    );
  }  
?>

protected by kiamlaluno May 16 '13 at 19:29

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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