WordPress:蒙版页面网址

时间:2019-01-25 13:03:16

标签: wordpress url-masking

我有一个表单,该表单会在提交时重定向并打开一个“谢谢”页面,但我希望没有人可以通过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
}

enter image description here

我不知道如何实现。有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

阻止直接访问“谢谢”页面的一种方法是确保到达那里的人实际上来自您的“联系我们”页面。

尝试将其添加到主题的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' );
相关问题