Dynamic thank you pages in WP Forms allow you to redirect users to a custom page after they submit a form. This can be useful in various scenarios, such as displaying a personalized message or offering a special offer to the user. To achieve this functionality, you can use the wpforms_process_complete
hook along with a custom PHP function.
Here’s an example code snippet that demonstrates how to redirect users to a dynamic thank you page based on the form ID:
/**
* Redirect users to a dynamic thank you page after form submission.
*
* @param array $fields
* @param array $entry
* @param array $form_data
*
* @return array
*/
function wpsnippets_redirect_thankyou_page( $fields, $entry, $form_data ) {
// Check if the form ID matches the desired form
if ( $form_data['id'] === 123 ) {
// Get the submitted form data
$name = $fields['name']['value'];
$email = $fields['email']['value'];
// Generate the dynamic thank you page URL
$thankyou_url = home_url( '/thank-you/?name=' . urlencode( $name ) . '&email=' . urlencode( $email ) );
// Redirect the user to the dynamic thank you page
wp_redirect( $thankyou_url );
exit;
}
return $fields;
}
add_filter( 'wpforms_process_complete', 'wpsnippets_redirect_thankyou_page', 10, 3 );
In this example, we’re using the wpforms_process_complete
hook to intercept the form submission process. The custom function wpsnippets_redirect_thankyou_page
is hooked to this action and receives three parameters: $fields
(an array of submitted form fields), $entry
(an array of entry data), and $form_data
(an array of form data).
Inside the function, we first check if the form ID matches the desired form (in this case, form ID 123). If it does, we retrieve the submitted form data (name and email) and generate a dynamic thank you page URL using home_url()
and urlencode()
functions. Finally, we redirect the user to the dynamic thank you page using wp_redirect()
and exit the script.
You can customize this code snippet by modifying the form ID, the thank you page URL structure, and the data you want to pass to the thank you page.
Examples
Example 1: Redirect to a Custom Thank You Page
This use case demonstrates how to redirect users to a custom thank you page after submitting a WP Forms form.
function wpsnippets_redirect_to_custom_thankyou( $url, $form_id ) {
if ( $form_id == 123 ) {
$url = 'https://example.com/thank-you';
}
return $url;
}
add_filter( 'wpforms_redirect_url', 'wpsnippets_redirect_to_custom_thankyou', 10, 2 );
The code example above uses the wpforms_redirect_url
filter to modify the redirect URL after form submission. In this case, if the form ID matches 123
, the user will be redirected to https://example.com/thank-you
instead of the default thank you page.
Example 2: Display a Custom Thank You Message
This use case demonstrates how to display a custom thank you message on the default thank you page after submitting a WP Forms form.
function wpsnippets_custom_thankyou_message( $message, $form_data ) {
if ( $form_data['id'] == 123 ) {
$message = 'Thank you for your submission! We will get back to you soon.';
}
return $message;
}
add_filter( 'wpforms_confirmation_message', 'wpsnippets_custom_thankyou_message', 10, 2 );
The code example above uses the wpforms_confirmation_message
filter to modify the default thank you message. If the form ID matches 123
, the custom message “Thank you for your submission! We will get back to you soon.” will be displayed instead.
Example 3: Send an Email with Custom Thank You Content
This use case demonstrates how to send an email with custom thank you content after submitting a WP Forms form.
function wpsnippets_send_custom_thankyou_email( $fields, $entry, $form_data ) {
if ( $form_data['id'] == 123 ) {
$to = 'example@example.com';
$subject = 'Thank you for your submission!';
$message = 'Dear ' . $fields['name'] . ', thank you for submitting the form. We appreciate your interest.';
wp_mail( $to, $subject, $message );
}
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_custom_thankyou_email', 10, 3 );
The code example above uses the wpforms_process_complete
action hook to send a custom thank you email. If the form ID matches 123
, an email will be sent to example@example.com
with the subject “Thank you for your submission!” and the message “Dear [name], thank you for submitting the form. We appreciate your interest.” The [name]
placeholder will be replaced with the actual submitted name.