【问题标题】:WordPress: Mask page URLWordPress:屏蔽页面 URL
【发布时间】:2019-01-25 13:03:16
【问题描述】:

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

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

【问题讨论】:

  • 如果设置了表单中的特定参数,您还可以检查您的感谢页面,如果没有设置,则假设有人尝试直接访问该页面并将他们重定向到您的主页。
  • 我已经设法安装了一个脚本,该脚本在以表单形式发送时会重定向到一个页面(谢谢页面),但我不知道该怎么做,这样如果您尝试从另一个站点进入,它将被重定向到主页。你能帮我解决这个问题吗?
  • 你能帮我解决这个问题吗?
  • 当然。不过,您需要显示您的代码。有关详细信息,请参阅How to create a Minimal, Complete, and Verifiable example
  • 什么代码?我在 .htaccess 中的代码不起作用,因为服务器没有读取文件,我不知道我还能做什么

标签: wordpress url-masking


【解决方案1】:

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

尝试将此添加到您主题的 functions.php 文件中,阅读 cmets 了解详细信息:

/**
 * 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' );

【讨论】:

  • 这是我的数据:$contact_page_ID=6206; $thank_you_page_ID=7177;但效果不佳,因为总是将我重定向到主屏幕,我已将您的代码粘贴到我的代码下方 (add_action( 'wp_footer', 'mycustom_wp_footer' );) 我怎样才能检查为什么不工作?
  • 试试这个: 1.启用error log; 2.在if ( $referer != $allowed_referer_url ) {之前添加error_log($referer . " " . $allowed_referer_url);; 3. 访问您的联系页面并使用联系表触发重定向; 4. 检查错误日志的内容。
  • 我在wp-content文件夹中创建了debug.log文件,我给了它660的权限,我配置了wp-config.php文件并激活了调试模式,但是它没有写任何东西在文件中对我来说,我看不到该文件中反映的任何错误
  • 尝试将权限设置为 644(或 755),然后再试一次。
  • 我放了777,从隐私浏览中测试了好几次,还是不行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多