Redirect Users to a Specific Page After Form Submission in WordPress
Explanation
Here's how you can redirect users to a specific page after they submit a form on your WordPress site.
Form Submission Handling:
- The code checks if the form is submitted using a special button name.
- It uses a security check called "nonce" to ensure the form submission is safe.
- After processing the form data (like cleaning up text), it redirects users to a page you choose.
- Change '/thank-you/' to the page you want users to see after submitting the form.
Form Display:
- The form is created with a simple text input and a submit button.
- A nonce field is included for security.
Using the Form:
- A shortcode is available to easily add the form to any page or post. Just use
[wp_dudecom_form]where you want the form to appear.
This setup ensures that after users fill out and submit the form, they are smoothly redirected to a page of your choice, like a "Thank You" page. Just remember to update the URL to match your desired destination!
Code
<?php
// Function to handle form submission and redirect
function wp_dudecom_handle_form_submission() {
// Check if the form is submitted
if ( isset( $_POST['wp_dudecom_form_submit'] ) ) {
// Verify nonce for security
if ( ! isset( $_POST['wp_dudecom_form_nonce'] ) || ! wp_verify_nonce( $_POST['wp_dudecom_form_nonce'], 'wp_dudecom_form_action' ) ) {
wp_die( 'Nonce verification failed!' );
}
// Process form data here
// Example: $form_data = sanitize_text_field( $_POST['form_field_name'] );
// Redirect to a specific page after form submission
$redirect_url = home_url( '/thank-you/' ); // Change '/thank-you/' to your desired page slug
wp_safe_redirect( $redirect_url );
exit;
}
}
add_action( 'init', 'wp_dudecom_handle_form_submission' );
// Function to display the form
function wp_dudecom_display_form() {
?>
<form method="post" action="">
<?php wp_nonce_field( 'wp_dudecom_form_action', 'wp_dudecom_form_nonce' ); ?>
<!-- Add your form fields here -->
<input type="text" name="form_field_name" required />
<input type="submit" name="wp_dudecom_form_submit" value="Submit" />
</form>
<?php
}
// Shortcode to display the form
function wp_dudecom_form_shortcode() {
ob_start();
wp_dudecom_display_form();
return ob_get_clean();
}
add_shortcode( 'wp_dudecom_form', 'wp_dudecom_form_shortcode' );
?>
Instructions
File Location: Add the code to your theme's functions.php file or a custom plugin file.
Prerequisites:
- Ensure you have access to your WordPress theme files or a custom plugin.
- Basic understanding of WordPress shortcodes and page slugs.
Implementation Steps:
- Open your WordPress admin dashboard.
- Navigate to Appearance > Theme Editor if you're using
functions.php, or go to Plugins > Editor if you're using a custom plugin. - Locate the
functions.phpfile or your custom plugin file. - Copy and paste the provided code into the file.
- Modify the
$redirect_urlvariable to point to your desired page slug, e.g.,'/thank-you/'. - Save the changes to the file.
- To display the form on a page or post, use the shortcode
[wp_dudecom_form]in the content editor where you want the form to appear. - Test the form by submitting it and ensure it redirects to the specified page.
This guide helps you set up a form that redirects users to a specific page after submission, enhancing user experience by guiding them to a "Thank You" page or any other page of your choice.
Need help with implementation or advanced functionality? Visit wp-dude.com for expert WordPress services.