• I’m trying to redirect users after they’ve logged in or registered back to the previous page but only if it was a product page.

    I have a digital download site that requires users to be logged in to buy products. There is a login/register button displayed for logged out users instead of the ‘Buy’ button as seen here: https://prnt.sc/1rc03lw

    If a user clicks the Login/Register button, they are directed to the ‘My Account’ page to be complete login or registration. Once complete, they are redirected back to the previous page (the product page they last visited).

    The PHP script I’m using only redirects the user to their previous page after login (not registration) and I would like the script to only run if they’ve come from a product page. If a user has logged in or registered from the homepage or my account page, for example, I’d like the user to be directed to the ‘My Account’ dashboard as normal.

    Here is the PHP script I’m using to redirect users after login:

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

    If anyone has any help or tips, I’d appreciate it. You can see the is_product() tag in the code above but, it doesn’t seem to function!

    • This topic was modified 3 years, 6 months ago by jamesjohnsonw.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirecting after login and registration IF coming from a product page’ is closed to new replies.