I have multiple contact forms in my wordpress website and have a different thankyou page for each. So, i want to prevent direct access of thankyou pages by just entering urls. I want if the user fills the form than it is only redirected to that forms thankyou page.
I have working code for 1 thankyou page but i don't know how to set up for multiple contact forms & thankyou pages.
// ID of the thank you page
if ( ! is_page(1911)) {
return;
}
// coming from the form, so all is fine
if (wp_get_referer() == '') {
return;
}
// we are on thank you page
// visitor is not coming from form
// so redirect to home
wp_redirect( get_home_url() );
exit;
} );```
Other thankyou page id's: 1269, 1825, 1623
Other contact form page urls: , ,
I have multiple contact forms in my wordpress website and have a different thankyou page for each. So, i want to prevent direct access of thankyou pages by just entering urls. I want if the user fills the form than it is only redirected to that forms thankyou page.
I have working code for 1 thankyou page but i don't know how to set up for multiple contact forms & thankyou pages.
// ID of the thank you page
if ( ! is_page(1911)) {
return;
}
// coming from the form, so all is fine
if (wp_get_referer() == 'https://www.example/contact') {
return;
}
// we are on thank you page
// visitor is not coming from form
// so redirect to home
wp_redirect( get_home_url() );
exit;
} );```
Other thankyou page id's: 1269, 1825, 1623
Other contact form page urls: https://www.example/contact-form-2, https://www.example/contact-form-3, https://www.example/contact-form-3
Share
Improve this question
asked Apr 17, 2020 at 5:29
Ujjawal PooniaUjjawal Poonia
214 bronze badges
1 Answer
Reset to default 1I have solved the issue by myself. I am posting the solution here in case if someone gets the same problem.
function wpse15677455_redirect() {
$ref = wp_get_referer();
if (is_page(1911) && $ref !== "https://www.example/contact"){
wp_redirect( get_home_url() );
}
else if(is_page(1269) && $ref !== "https://www.example/contact-form-2"){
wp_redirect( get_home_url() );
}
else if(is_page(1825) && $ref !== "https://www.example/contact-form-3"){
wp_redirect( get_home_url() );
}
else if(is_page(1623) && $ref !== "https://www.example/contact-form-4"){
wp_redirect( get_home_url() ); exit();
};