WordPress:屏蔽页面 URL [英] WordPress: Mask page URL

查看:19
本文介绍了WordPress:屏蔽页面 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form that redirects and opens a "thank you" page on submission, but I would like no one to have access to this page by URL. This page should be accessible from the redirection of the form. The problem is that I tried to do it from .htaccess, but it does not work.

当前网址:mySite.com/thank-you

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

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

I used this code in 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
}

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

I don't know how to make it possible. Can someone help me please?

推荐答案

防止直接访问谢谢"页面的一种方法是确保到达那里的人确实来自您的联系我们"页面.

One way to prevent direct access to your "Thank You" page is by making sure that the people who get there actually came from your "Contact Us" page.

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

Try adding this to your theme's functions.php file, read the comments for details:

/**
 * 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 版本实现了相同的结果(应该更兼容缓存插件):

Alternatively, this JavaScript version achieves the same result (which should be more compatible with caching plugins):

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:屏蔽页面 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆