如果评论来自注册页面,则在评论提交后重定向

时间:2022-01-15 作者:Marcus Wood

在成功地将WordPress评论表单的自定义版本转换为;“注册”;页面,我现在需要在提交;“表格”;。

页面已完成,页面的slug为thank-you-for-signing-up. 问题是,如何检查评论是否来自sign-up 页面(slug:注册),如果是,请重新指向我的自定义页面?

This is the code I am trying to get working:

add_filter(\'comment_post_redirect\', \'sign_up_redirect\');
function sign_up_redirect( $location ) {

        $signup_redirect = wp_redirect( home_url(\'/thank-you-for-signing-up/\') );

    if ( is_page( \'sign-up\' ) ) {

    return $signup_redirect;

    exit;

        }
}

The signup template:

<?php
/**
* Template Name: Signup
*/
get_header();
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="container">
To recieve updates and news, sign up using the form below.
We\'ll send over your account details within 24 hours.
<div style="margin-bottom:50px;"></div>
<?php
if (is_user_logged_in() && current_user_can(\'administrator\')){
$qanda_info = $wpdb->get_results("SELECT DISTINCT comment_date_gmt, comment_author, comment_author_email, comment_author_IP
FROM (SELECT DISTINCT comment_date_gmt, comment_author, comment_author_email, comment_author_IP
FROM dev_comments
WHERE comment_post_ID = 70
) AS QANDA");
echo \'<div style="margin-top:50px;"></div><div style="padding:20px;background-color:#fff;border:3px solid #333;border-radius:5px;"><ul>\';
foreach($qanda_info as $info){
echo \'<li><strong>\'. $info->comment_date_gmt .\'</strong> | \'. $info->comment_author .\' | \'. $info->comment_author_email .\' | IP-number: <small>\'. $info->comment_author_IP .\'</small></li>\';
}
echo \'</ul></div>\';
}
comments_template(\'/signups.php\');
?></div><?php
endwhile; endif;
get_footer();

The signups "comment" form:

<?php if (post_password_required()) return;
$signup_args = array(
\'label_submit\' => __(\'Sign Up\', \'textdomain\')
);
comment_form($signup_args);
仅此而已。

1 个回复
SO网友:Sally CJ

This is the code I am trying to get working:

首先,让我们回顾一下代码中的错误:

本部分:$signup_redirect = wp_redirect( home_url(\'/thank-you-for-signing-up/\') );, 应该是$signup_redirect = home_url(\'/thank-you-for-signing-up/\'); 因为不然wp_redirect() 将用户重定向到您的;“谢谢你”;页面,无论评论是从何处提交的。

但你真的不应该打电话wp_redirect() 在过滤器钩子中(应该总是像使用短代码一样返回某些内容)。如果要重定向,请在挂钩中执行。

这部分:if ( is_page( \'sign-up\' ) ), 这永远不会是真的,因为你在使用comment_form() 它(默认情况下)将表单提交给/wp-comments-post.php 激发了comment_post_redirect hook.

所以在那一页上,is_page() 永远都是false.

现在,关于重定向到您的自定义;“谢谢你”;页面成功提交评论后,您可以尝试以下使用wp_get_referer() 获取引用URL及其路径是否/sign-up/ 如中所示https://example.com/sign-up/, 然后修改重定向URL:

function sign_up_redirect( $location ) {
    $referer = wp_get_referer();

    // The referer might be a relative path, so check that before calling parse_url().
    $ref_url_path = ( \'/sign-up/\' === $referer ) ? $referer :
        parse_url( $referer, PHP_URL_PATH );

    // If it\'s the sign-up page, modify the redirect URL. Else, return it unmodified.
    return ( \'/sign-up/\' === $ref_url_path ) ?
        home_url( \'/thank-you-for-signing-up/\' ) : $location;
}
为了提高逻辑的可靠性,您应该在注释表单中添加一个referer隐藏字段,您可以通过comment_form hook, like so:(参见wp_referer_field() 有关函数的更多详细信息)

add_action( \'comment_form\', \'sign_up_comment_form\' );
function sign_up_comment_form() {
    if ( is_page( \'sign-up\' ) ) {
        wp_referer_field();
    }
}
/* Or to add it to all comment forms:
add_action( \'comment_form\', \'wp_referer_field\' );
*/
PS:将代码添加到主题的functions.php 文件

因此,我希望这会有所帮助,只需查看我的答案的最新版本,就可以尝试其他可能的选项:)

相关推荐

Redirect Css files to https

在哪里可以找到负责调用Css文件的文件,并测试它们的链接是http还是https?我想测试请求是否为https,如果是,那么很好,如果不是,请将其更改为https在函数中。php我找到了负责调用主题Css的函数,下面是它的代码:if (!function_exists(\'jobcareer_front_scripts\')) { function jobcareer_front_scripts() { global $jobcareer_options;