Redirect user to previous page after signup from custom form

I've looked on here and found lots of references to this, but none have worked for me, probably because there must be something in the custom code I am modifying that I don't quite understand. Having taken data from pervious posts here is the original parts of the code that I think I needed to modify: Line 124

// Only show the registration form to non-logged-in members.
    if ( ! is_user_logged_in() ) {
        add_action( 'wp_footer', 'wyz_add_pass_strength_script' );

There is then a large amount of code for the sign up form which results at the end in the following Line 739

$creds = array();
$creds['user_login'] = $registration_data['user_login'];
$creds['user_password'] = $registration_data['user_pass'];
$creds['remember'] = true;
$user = wp_signon( $creds, is_ssl() );

$url = apply_filters( 'wyz_after_login_redirect', home_url( '/user-account/' ), $user, true );
$url = apply_filters( 'wyz_after_register_redirect', $url, $user );
// Send the newly created user to the user account page after logging him in.
wp_redirect( $url );
exit;

The current code takes the user to the account page as stated in the code comment. I want to take the user back to the original page so I added

$redirect = home_url() . '?redirect_to=' . esc_url($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);

At Line 126 And modified to

wp_redirect ($redirect)

before the exit. It didn't work. I kinda expected it wouldn't but I'm not sure what to do now. I've left out the rest of the code because I wanted to keep this query succinct, but if it's required, please let me know and I'll edit the post.

Topic signup wp-redirect redirect login Wordpress

Category Web


I'm having a little trouble following the context, but it looks like all you have to do is make use of the filters that are provided in the code you're looking at.

$url = apply_filters( 'wyz_after_register_redirect', $url, $user );

This hook gives you the chance to modify the redirect url before the redirect is performed. You can take advantage of this to send the users where you want them to go.

add_filter( 'wyz_after_register_redirect', 'my_after_register_redirect', 99, 2 );
function my_after_register_redirect( $url, $user ) {
    return home_url( $_SERVER['REQUEST_URI'] );
}

You can register your hook by using add_filter for your own function, my_after_register_redirect. This will fire the above code when the apply_filters for wyz_after_register_redirect is called. The returned string will be used in place of the $url value.

NOTE: Keep in mind, the Request URI may not be the url you want users to be redirected to. You may have to work around this in another way.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.