Home  >  Q&A  >  body text

After successful registration, the user will be directed to the page he came from.

I am using the following code to redirect the user to the page they came from after successful login. This code has been tested and works fine:

add_action( 'woocommerce_login_form_end', 'actual_referrer' );
 
function actual_referrer() {
   if ( ! wc_get_raw_referer() ) return;
   echo '<input type="hidden" name="redirect" value="' . wp_validate_redirect( wc_get_raw_referer(), wc_get_page_permalink( 'myaccount' ) ) . '" />';
}

Now, when the user enters my-account from the registration page to register, after the registration is successful, I want to return to his original page, such as the code above.

For this, I found a code that redirects the user to the home page after successful registration:

add_filter( 'woocommerce_registration_redirect', 'customer_register_redirect' );
 
function customer_register_redirect( $redirect_url ) {
   $redirect_url = get_home_url();  
   return $redirect_url;
}

Is there a way to edit this code like the login code to redirect the user to the page they came from?

P粉107991030P粉107991030431 days ago493

reply all(1)I'll reply

  • P粉476475551

    P粉4764755512023-09-08 00:30:00

    You can use wp_registration_url( urlencode( get_permalink() ) );, and you will be redirected to the previous page after successful registration.

    In your case, since it is woocommerce, the link to your page should be:

    <a href="<?php echo wc_get_page_permalink( 'myaccount' ) . '?redirect_to=' . urlencode( get_permalink() ); ?>">Register</a>

    Where wp_registration_url is the URL of the registration page, and add the ?redirect_to parameter (including the current page).

    Depending on your situation, you can change the code in the filter to:

    add_filter( 'woocommerce_registration_redirect', 'customer_register_redirect' );
    function customer_register_redirect() {
        $redirect_url = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : home_url();
        return $redirect_url;
    }

    If redirect_to is not set, we will redirect to the home page or another page you like.

    reply
    0
  • Cancelreply