Redirect node forms to different URLs in Drupal 7

Profile picture for user Phil Frilling
By Phil Frilling, 27 September, 2012
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:

function MYTHEME_form_alter(&$form, &$form_state){
  // Incorrect for node forms.
  $form['#redirect'] = base_path() . 'my-new-path';
}
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.

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