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
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.