After having successfully done a custom version of the WordPress comments form into a "sign up" page, I now need to re-direct the user to a custom page upon submitting the "form".

The page is done and the slug of the page is thank-you-for-signing-up. Problem is, how do I check that the comments is coming from the sign-up page (slug: sign-up) and if so, re-direct to my custom page?

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;



The signup template:

* Template Name: Signup
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>
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
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>';
endwhile; endif;

The signups "comment" form:

<?php if (post_password_required()) return;
$signup_args = array(
'label_submit' => __('Sign Up', 'textdomain')

That's all.

  • I'm going to assume the wp_redirect() and exit calls were just typos in the question... but your code would still not work, anyway.. so can you show the code you used for adding the comments form? Are you using a specific/custom Page template for the sign-up page? If so, can we see the full code?
    – Sally CJ
    Jan 17 at 3:59
  • Of ocurse, I can post that. Can you please explain what you mean about "typos"? Jan 17 at 5:56
  • @SallyCJ updated as requested. Jan 17 at 5:59
  • I was just saying you shouldn't call those in a filter hook like comment_post_redirect. Anyway, I'll check your updates and let you know later on.
    – Sally CJ
    Jan 17 at 6:39
  • Ah, okay. I hope that my question is clear, as what I need to do is check if the page is "signup" and if so and if someone posts a "comment" on that page - only then re-direct them to the "thanks for signing up" page. Jan 17 at 6:51

This is the code I am trying to get working:

Firstly, let's review the mistakes in your code:

  1. This part: $signup_redirect = wp_redirect( home_url('/thank-you-for-signing-up/') );, should be $signup_redirect = home_url('/thank-you-for-signing-up/'); because otherwise wp_redirect() would redirect the user to your "thank you" page, regardless from where the comment was submitted.

    But you really shouldn't call wp_redirect() in a filter hook (which should always return something just as with shortcodes). If you wanted to redirect, do it in an action hook.

  2. And this part: if ( is_page( 'sign-up' ) ), it would never gonna be true because you're using comment_form() which (by default) submits the form to /wp-comments-post.php that fires the comment_post_redirect hook.

    So on that page, is_page() would always be false.

And now, as for redirecting to your custom "thank you" page upon successful comment submission, you can try the following which uses wp_get_referer() to get the referring URL and if its path is exactly /sign-up/ as in https://example.com/sign-up/, then we modify the redirect 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;

And to increase the reliability of the logic, you should add a referer hidden field to your comment form, which you can do via the comment_form hook, like so: ( see wp_referer_field() for more details about the function )

add_action( 'comment_form', 'sign_up_comment_form' );
function sign_up_comment_form() {
    if ( is_page( 'sign-up' ) ) {
/* Or to add it to all comment forms:
add_action( 'comment_form', 'wp_referer_field' );

PS: Add the code to the theme's functions.php file.

So I hope that helps and just view the last revision of my answer for the other possible options you could try.. :)

  • Thanks for your efforts, but it does not work. All I get is the "Please write a comment" error in WP. Or should I use the sign_up_redirect function with an add_action? Jan 19 at 9:34
  • Thanks for letting me know, and no, the hook is a filter, so add_filter() should be used. So try reverting all your changes (from my original answer) and then add the code in my answer to your functions file.
    – Sally CJ
    Jan 19 at 9:43
  • Sorry for not being clear, I am using the code from this new updated answer and still, the "same" error. Jan 19 at 10:05
  • In that case, then maybe there's something in your theme (or maybe a plugin) which is messing with the comment form fields - if you removed both the code in my answer, does the same issue persist? Have you tried clearing your caches and deactivating plugins? And is it okay for you to share (on Pastebin.com) the HTML of the raw/server-generated form code, i.e. on Chrome (desktop), press the ctrl and u keys simultaneously.
    – Sally CJ
    Jan 19 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.