< Back to Guides

CF7 – Change Form Action

When you want to practise good userflows in your website design, you’ll find that a simple thank you message on the form isn’t necessarily good enough. Instead, redirecting that user to another page will give a better user experience.

The code below shows how to direct ALL contact form 7 forms to another page:

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url){
  return 'http://YOUR URL HERE;
}

However, sometimes you’ll want to do this on a form by form basis, in which case you can test against the form ID and then use this to redirect to any relevant page you want.

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url){
  // Check if the current form exists
  if (WPCF7_ContactForm::get_current()) {
    // Get the ID of the current form
    $form_id = WPCF7_ContactForm::get_current()->id();
    // Check if the form ID is YOUR-FORM-ID
    if ($form_id == YOUR-FORM-ID) {
      return 'http://YOUR URL HERE';
    }
  }
  // If not form 420, return the original URL
  return $url;
}

If you wanted to get extra clever, you could also check against a hidden value in the form which tells you where to redirect the user.