$_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();
}