Wordpress

WP-Cred login form: redirects based on different member roles

/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}
 
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect

function my_login_redirect( $redirect_to, $request, $user ) {
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
        return $redirect_to;
    }
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {
        //check for subscribers
        if (in_array('subscriber', $user->roles)) {
            // redirect them to another URL, in this case, the homepage
            $redirect_to =  '<em><u>hidden link</u></em>';
        }
 
        if (in_array('contributor', $user->roles)) {
            // redirect them to another URL, in this case, the search page
            $redirect_to = '<em><u>hidden link</u></em>';
        }
 
        // copy and paste the block above for each role
    }
 
    return $redirect_to;
}
 
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

https://toolset.com/forums/topic/need-to-have-1-login-form-that-redirects-based-on-wp_user-role/

Scroll al inicio

We are using cookies on our website

Please confirm, if you accept our tracking cookies. You can also decline the tracking, so you can continue to visit our website without any data sent to third party services.