最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp redirect - how to block direct access to multiple thank you pages?

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

I 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();
 };
发布评论

评论列表(0)

  1. 暂无评论