WordPress的:屏蔽网页URL(WordPress: Mask page URL)

2019-11-05 11:08发布

我有一个重定向,并在提交打开一个“谢谢”页形式,但我想没有人有通过URL访问该页面。 此页面应该从形式的重定向访问。 问题是,我试图从做.htaccess ,但它不工作。

当前网址: 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
}

我不知道如何使之成为可能。 有人能帮助我吗?

Answer 1:

以防止你的“谢谢”页面直接访问的一种方式是通过确保谁那里的人们真正从“联系我们”页面来了。

尝试添加这对你的主题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' );


文章来源: WordPress: Mask page URL