Redirect after sign up with woocommerce form with role choice

admin2025-01-07  6

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 7 days ago.

Improve this question

I use the woocommerce registration form. The user registers and must choose the role. I have 2 user roles and I need to make a redirect for the individual roles. Example: user ONE chooses the role "customer", redirect to the url user TWO chooses the role "endorser", redirect to the url . I have tried several plugins but none of them work. Can anyone help me? Thanks

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 7 days ago.

Improve this question

I use the woocommerce registration form. The user registers and must choose the role. I have 2 user roles and I need to make a redirect for the individual roles. Example: user ONE chooses the role "customer", redirect to the url https://mysite.com/shop user TWO chooses the role "endorser", redirect to the url https://mysite.com/landingpage. I have tried several plugins but none of them work. Can anyone help me? Thanks

Share Improve this question asked Dec 28, 2024 at 19:52 MaxMax 132 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

user_register hook fires immediately after a new user is registered. so you have to add action on this hook. You can add below example code in your active theme's function file.

Example :

add_action( 'user_register', 'redirect_user_based_on_role', 10, 1 );

function redirect_user_based_on_role( $user_id ) {

    // Get the user object.
    $user = get_userdata( $user_id );

    // Get all the user roles as an array.
    $user_roles = $user->roles;

    // Check if the role you are interested in, is present in the array.
    if ( in_array( 'customer', $user_roles, true ) ) {

          // Do something.
          wp_redirect( 'https://mysite.com/shop' );
          exit;

    }
    
    if ( in_array( 'endorser', $user_roles, true ) ) {

          // Do something.
          wp_redirect( 'https://mysite.com/landingpage' );
          exit;

    }

}

This might be a case where careful use of hooks comes in. That's assuming you want to do this in a plugin of your own or the functions.php file of your theme. If you just want an existing plugin or feature in whatever you are currently using, that'd be a bit out of scope here - the registration plugin author might be able to help perhaps.

As this is WordPress Development SE, here is the developer answer.

I don't know what exactly is going on for your set-up as it is unclear what you are running to do the register and chose role system you have.

However, it is very likely that there will be a hook that fires. That hook could be used with a register_action to push some sort of redirect logic.

For example, user_register fires immediately a user is added to the database. Another possibility is register_new_user.

add_user_role fires when a new role is added. This is where I would start looking if it were me.

All of these can be jumped on via the add_action() function using something along the lines of:

 add_action('add_user_role','my_new_function',10,1);

where my_new_function is a function you made. The exact details will depend where you are putting the code.

I would probably add a call to wp_redirect() which is the WordPress way to redirect users.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255268a268.html

最新回复(0)