• Resolved ihopethiswillfi

    (@ihopethiswillfi)


    Hi

    I’m trying to pass a query variable to the registration form so that I can have a dynamic redirect upon registration completion.

    My link to the registration form looks something like:
    https://mywebsite.com/register/?product=2

    And the goal is that after registration completes, the user is redirected to:
    https://mywebsite.com/products/?product=2

    functions.php:

    
    add_filter( 'query_vars', 'add_query_vars_filter' );
    function add_query_vars_filter( $vars ) {
      $vars[] = 'product';
      return $vars;
    }
    
    add_action( 'wpmem_register_redirect', 'my_reg_redirect' );
    function my_reg_redirect()
    {
        if ( get_query_var('product') != '') {
    		$to_append = '?product=' . get_query_var('product');
    		wp_redirect( 'https://mywebsite.com/products/' . $to_append );
    	} else {
    		wp_redirect( 'https://mywebsite.com/');
    	}
    	exit();
    }

    The problem is that get_query_var(‘product’) always returns an empty string.

    Is it possible that the logic behind the registration form somehow prevents detection of the query parameters?

    I wrote my first line of php about a week ago, so I hope this isn’t a stupid question ??

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Chad Butler

    (@cbutlerjr)

    You actually can’t use get_query_var() that way, which is at least part of why your code isn’t working. Use $_GET instead. (Alternatively, you could use wpmem_get() which is a function in the WP-Members API).

    Also, make sure you sanitize the variable and escape the URL before redirecting like this:

    $to_append = '?product=' . sanitize_text( $_GET['product'] );
    wp_redirect( esc_url( 'https://mywebsite.com/products/' . $to_append ) );

    Actually… I’d take it a step further and just use WP’s add_query_arg() function to add the query string to the URL. And while we’re at it… be sure to check if the $_GET var is actually set instead of checking if it is empty – otherwise you could get an undefined error. And use home_url() to generate the URL instead of hard coding it to make your script a little more portable. And if the redirect is part of the local site, use wp_safe_redirect() instead.

    if ( isset( $_GET['product'] ) ) {
        $to_append = sanitize_text( $_GET['product'] );
        $url = add_query_arg( 'product', $to_append, home_url( '/products/' ) );
        wp_safe_redirect( esc_url( $url ) );
    } // etc...
    Thread Starter ihopethiswillfi

    (@ihopethiswillfi)

    Hi @cbutlerjr

    Thanks, that reply surely was more than I expected and I learned a few things there.

    Unfortunately there’s more to it ??

    So even after using your code, I found that $_GET[‘product’] always returns an empty string on the registration page after using the hook ‘wpmem_register_redirect’.

    I then decided to try to extract the query string on the register page itself, rather than with the above-mentioned hook.

    So I inserted below 2 lines into the registration page with plugins PHPeverywhere and WidgetLogic:

    error_log('Loading registration page.');
    error_log($_GET['product']);

    And there I’m suddenly able to catch the parameter value!

    So to sum it up,
    on the registration page itself I’m able to get the query var value.
    when I use the hook, the query var is always an empty string.

    Is there a ‘cleaner’ way to do things than injecting extra code into the registration page?

    Plugin Author Chad Butler

    (@cbutlerjr)

    $_GET[‘product’] always returns an empty string on the registration page after using the hook ‘wpmem_register_redirect’.

    Yeah… that would make sense. I must have answered this late and not thought it through. The redirect hook is at the end of registration processing, so by the time you’re at wpmem_register_redirect, the form has already been submitted. $_GET vars are not passed through form submission when a form submits back to itself.

    You’d have to pick up the vars while generating the form and pass them with form submission. You could do that via $_GET variables, but I think it’s probably better to do it via $_POST.

    I would suggest that you use the wpmem_register_hidden_fields filter to add your variable to the hidden fields and then it will pass with the $_POST vars in the form:

    add_filter( 'wpmem_register_hidden_fields', function( $str ){
    	
    	if ( isset( $_GET['product'] ) ) {
    		$product = sanitize_text( $_GET['product'] );
    		$str.= '<input name="product" type="hidden" value="' . $product . '" />';
    	}
    	
    	return $str;
    });

    That will add it to the form and you can then change $_GET in your redirect script to $_POST and it should pick it up.

    THAT would do it… However, there are a few other things to note but from a clean code process and general plugin functionality that I didn’t think of when I posted my original answer.

    The plugin itself runs the wpmem_register_redirect action to handle the default form redirection if a “redirect_to” query string is passed. This would occur with a shortcode generated form if a redirect_to attribute was specified. You’re probably not using this, but just in case for forward compatibility, you should specify an earlier priority for your hook so it kicks in before the plugin’s. Note in my code below the “5” for priority so that it comes earlier than the plugin’s.

    Next, if you simply redirect, there will be no success message. This is because you exit the process and move the user so the return from the registration function is lost. There’s a way around that, however, and that is to pass reg_nonce in the query string. (The plugin does this same thing with the redirect_to parameter mentioned above.) This of course is optional because I don’t know your specifics, but I would assume you’d want a “registration success” message to display. I noted the line in the code below so you could just take it out if it doesn’t work to your liking. But I note it here because even if you don’t use it, someone reading this later might.

    Lastly, I compacted your “if” to just generate the URL. You can then apply wp_safe_redirect() based on that result. It just tightens things up a bit. (And note my change to $_POST relative to the code snippet above.)

    add_action( 'wpmem_register_redirect', 'my_reg_redirect', 5 );
    function my_reg_redirect( $url ) {
    	
    	if ( isset( $_POST['product'] ) ) {
    		$to_append = sanitize_text( $_POST['product'] );
    		$url = add_query_arg( 'product', $to_append, home_url( '/products/' ) );
    	} else {
    		$url = home_url();
    	}
    	
    	// Try it with this to pass the nonce.
    	$nonce_url = wp_nonce_url( $url, 'register_redirect', 'reg_nonce' );
    	
    	wp_safe_redirect( esc_url( $nonce_url ) );
    	exit();
    }
    Thread Starter ihopethiswillfi

    (@ihopethiswillfi)

    Silly me, I had totally forgotten that the user should first login before being redirected :). So I took care of that by passing the queryvar from the registration page to the login page in the same way.

    So the process looks like this:
    – logged out user clicks a specific product on the product page.
    – user is directed to the register page.
    – after registering, user is directed to the login page.
    – after logging in, user is sent back to the product he originally came from.

    This all works perfectly now except one thing: the registration success message does not show. It’s a rather important message cause if the credential emails ends up in spam, the user has no way of knowing how to proceed.

    This is the URL I see in the address bar after registering and getting to the login page:
    https://mywebsite.com/login/?product=10thproduct#038;reg_nonce=61aa897420

    Before I created the manual redirect to the login page, the nonce did work. So I’d click on ‘register’ and be automatically forwarded to the login page (default wp-members behavior) and see the success message. (side note: below the success message there was also a message that I don’t have access to that page).

    So it seems the reason that the success message isn’t working is my manual redirect to /login/.

    Hmmm if this can’t be solved then I could also opt to add a permanent header to the login page “login and password were emailed to you during registration”. Then it’s not such a big problem that the success message is failing to show.

    Anyway, all of the code below. Maybe it will help someone in the future.

    add_filter( 'wpmem_register_hidden_fields', function( $str ){
    	if ( isset( $_GET['product'] ) ) {
    		$product = sanitize_text_field( $_GET['product'] );
    		$str.= '<input name="product" type="hidden" value="' . $product . '" />';
    	}
    	return $str;
    });
    
    add_action( 'wpmem_register_redirect', 'my_reg_redirect', 5 );
    function my_reg_redirect( $url ) {
    	if ( isset( $_POST['product'] ) ) {
    		$to_append = sanitize_text_field( $_POST['product'] );
    		$url = add_query_arg( 'product', $to_append, home_url( '/login/' ) );
    	} else {
    		$url = home_url('/login/');
    	}
    
    	// Try it with this to pass the nonce.
    	$nonce_url = wp_nonce_url( $url, 'register_redirect', 'reg_nonce' );
    	wp_safe_redirect( esc_url( $nonce_url ) );
    	exit();
    }
    
    add_filter( 'wpmem_login_hidden_fields', function( $str ){
    	if ( isset( $_GET['product'] ) ) {
    		$product = sanitize_text_field( $_GET['product'] );
    		$str.= '<input name="product" type="hidden" value="' . $product . '" />';
    	}
    	return $str;
    });
    
    add_filter( 'wpmem_login_redirect', 'my_login_redirect', 10, 2 );
    function my_login_redirect( $redirect_to, $user_id ) {
    	if ( isset( $_POST['product'] ) ) {
    		$to_append = sanitize_text_field( $_POST['product'] );
    		$url = add_query_arg( 'product', $to_append, home_url( '/products/' ) );
    	} else {
    		$url = home_url();
    	}
        return esc_url($url);
    }
    Plugin Author Chad Butler

    (@cbutlerjr)

    I think you might have lost me there, but sounds like you got things working the way you wanted?

    Thread Starter ihopethiswillfi

    (@ihopethiswillfi)

    Yes, it all works, except I have no success message after registration. (the nonce doesn’t work, probably because of me manually redirecting to the login page after registration). That’s a slight inconvenience I can work around.

    Thanks for the best support I could wish for ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Passing parameter to registration form’ is closed to new replies.