How To Add Thank You Page In Contact Form 7

Including a gratitude page on your Contact Form 7 is an excellent method of adding a customized touch for those visiting your website. In this piece, I will assist you in the steps of incorporating a thank you page to your Contact Form 7 and also offer my own personal tips and tricks.

Step 1: Install and Activate Contact Form 7 Plugin

Before we start, make sure you have the Contact Form 7 plugin installed and activated on your WordPress website. You can do this by navigating to your WordPress dashboard, clicking on “Plugins” in the left-hand menu, and then selecting “Add New”. Search for “Contact Form 7”, and once you find it, click on “Install Now” and then “Activate”.

Step 2: Create and Configure Your Contact Form

Once you have activated the Contact Form 7 plugin, you can create and configure your contact form. Go to the “Contact” section in your WordPress dashboard and click on “Contact Forms”. Here, you can create a new form or edit an existing one. Customize the form fields according to your requirements, such as name, email, subject, and message fields.

Adding a thank you page requires a little bit of coding knowledge, so let’s dive into the next steps.

Step 3: Add Custom Code to Your Functions.php File

To add a thank you page, we need to modify the functions.php file of your WordPress theme. Before making any changes, it’s always a good practice to create a backup of your website.

Open your WordPress dashboard, go to “Appearance” > “Theme Editor”. In the Theme Editor, locate the functions.php file on the right-hand side and click on it to open it for editing.

Once the functions.php file is open, add the following code at the end:

function my_wpcf7_redirect() {
if ( isset( $_POST['_wpcf7'] ) ) {
$submission = WPCF7_Submission::get_instance();
$cf7 = wpcf7_contact_form( $_POST['_wpcf7'] );
if ( $cf7 ) {
$wpcf7 = \WPCF7_ContactForm::get_current();
$submission = \WPCF7_Submission::get_instance();
$_referer = $submission->get_meta( 'referer_url' );
$url = 'https://example.com/thank-you-page'; // Replace with your thank you page URL
if ( ! empty( $_referer ) && false !== strpos( $_referer, home_url() ) ) {
wp_redirect( $url );
exit;
}
}
}
}
add_action( 'wp', 'my_wpcf7_redirect' );

Make sure to replace ‘https://example.com/thank-you-page’ with the actual URL of your thank you page. Once you have added the code, click on the “Update File” button.

Step 4: Test Your Contact Form

Now that you have added the custom code to your functions.php file, it’s time to test your contact form. Go to the frontend of your website and submit the contact form. You should be redirected to the thank you page specified in the code.

Conclusion

Adding a thank you page to your Contact Form 7 can enhance the user experience on your website by providing a personalized touch. By following the steps outlined in this article and adding the custom code to your functions.php file, you can easily redirect visitors to a thank you page after they submit a form. Remember to always test your contact form to ensure everything is working as expected.