skip to Main Content

I have a website that uses WordPress and Woocommerce and i want to redirect the user to a custom URL after he do log in with the WooCommerce login page.

I’m new to programming and wordpress, sorry if my question looks dumb

I searched for the solution in the internet and found this:

function my_login_redirect( $redirect_to, $request, $user ) {
        $redirect_to = 'CustomURLhere';
    return $redirect_to;
}
add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 10, 3 );

but it didn’t work, it gave me a message that my website have an error

2

Answers


  1. i think you don’t need to use "$request" and "$user" for login redirection, just try to add this code at the bottom of your functions.php file:

    add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 9999 );
    function my_login_redirect( $redirect_to) {
       $redirect_to = 'CustomURLhere'; // don't forget to change this with your URL ;)
       return $redirect_to;
    }
    

    Hope it helps !

    Login or Signup to reply.
  2. To redirect a user to a specific page after they log in to your WooCommerce website, you can use the woocommerce_login_redirect filter. Here’s an example of how to use it:

    1. Open your website’s functions.php file (you can find it in your WordPress theme’s directory).

    2. Add the following code to the file:

    function my_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
        // Change this to the URL of the page you want to redirect the user to
        $redirect_url = home_url( '/my-account/' );
    
        // If the user is a subscriber, redirect them to the specified URL
        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
            if ( in_array( 'subscriber', $user->roles ) ) {
                $redirect_to = $redirect_url;
            }
        }
    
        return $redirect_to;
    }
    add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 10, 3 );
    

    3. Replace /my-account/ with the URL of the page you want to redirect the user to after they log in.

    4. Save the changes to the functions.php file.

    Now, when a user logs in to your WooCommerce website, they will be redirected to the page you specified. Note that this code only redirects users with the "subscriber" role. You can modify the code to redirect users with different roles or to redirect all users.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search