Skip to main content

Redirect node forms to different URLs in Drupal 7

Today I needed to redirect a node form submission to a separate URL. First, I tried the standard form api #redirect option in the form array like this:

  1. function MYTHEME_form_alter(&$form, &$form_state){
  2.   // Incorrect for node forms.
  3.   $form['#redirect'] = base_path() . 'my-new-path';
  4. }

That didn't work. Then I remembered I came across a similar problem while trying to redirect the user login form to a different URL. Using that code worked like a charm.

  1. function MYTHEME_form_alter(&$form, &$form_state){
  2.   // This works for node forms.
  3.   // Explode the form action and get the query string.
  4.   $url = explode('?', $form['#action']);
  5.  
  6.   // Replace the query string with the destination I want.
  7.   $form['#action'] = $url[0] . '?destination=my-new-path';
  8. }


Comments