WordPress:屏蔽页面 URL

WordPress: Mask page URL

我有一个表单可以在提交时重定向并打开 "thank you" 页面,但我不希望任何人通过 URL 访问此页面。该页面应该可以从表单的重定向访问。问题是我尝试从 .htaccess 执行此操作,但它不起作用。

当前 URL:mySite.com/thank-you

我想将其屏蔽为:mySite.com/

我在function.php中使用了这段代码:

/**
 * Form ---> Thank you page
 *
 */
add_action( 'wp_footer', 'mycustom_wp_footer' );

function mycustom_wp_footer() {
    ?>
    <script type="text/javascript">
        document.addEventListener( 'wpcf7mailsent', function( event ) {
            if ( '6881' == event.detail.contactFormId ) { // Sends sumissions on form idform to the thank you page
                location = '/thank-you/';
            } else { // Sends submissions on all unaccounted for forms to the third thank you page
                // Do nothing
            }
        }, false );
    </script>
    <?php
}

我不知道如何让它成为可能。有人可以帮我吗?

防止直接访问您的 "Thank You" 页面的一种方法是确保到达那里的人确实来自您的 "Contact Us" 页面。

尝试将其添加到您主题的 functions.php 文件中,阅读评论了解详情:

/**
 * Redirects user to homepage if they try to
 * access our Thank You page directly.
 */
function thank_you_page_redirect() {
    $contact_page_ID = 22; // Change this to your "Contact" page ID
    $thank_you_page_ID = 2; // Change this to your "Thank You" page ID

    if ( is_page($thank_you_page_ID) ) {
        $referer = wp_get_referer();
        $allowed_referer_url = get_permalink( $contact_page_ID );

        // Referer isn't set or it isn't our "Contact" page
        // so let's redirect the visitor to our homepage
        if ( $referer != $allowed_referer_url ) {
            wp_safe_redirect( get_home_url() );
        }
    }
}
add_action( 'template_redirect', 'thank_you_page_redirect' );

更新:

另外,这个 JavaScript 版本也能达到同样的效果(应该更兼容缓存插件):

function mycustom_wp_head() {
    $home_url = get_home_url();
    $contact_page_ID = 22; // Change this to your "Contact" page ID
    $thank_you_page_ID = 2; // Change this to your "Thank You" page ID

    if ( is_page($thank_you_page_ID) ) :
    ?>
    <script>
        var allowed_referer_url = '<?php echo get_permalink( $contact_page_ID ); ?>';

        // No referer, or referer isn't our Contact page,
        // redirect to homepage
        if ( ! document.referrer || allowed_referer_url != document.referrer ) {
            window.location = '<?php echo $home_url; ?>';
        }
    </script>
    <?php
    endif;
}
add_action( 'wp_head', 'mycustom_wp_head' );