简体   繁体   中英

How to redirect to my specific URL containing username - Wordpress

I've posted the code below. Whenever I logged in, the $username is not getting executing thus leaving blank.

function my_login_redirect( $redirect_to, $request, $user ) {
   global $current_user;
$current_member = wp_get_current_user();
$username = $current_member->user_login;
    $url = home_url( "/connections/$username/profile/edit/group/1/" ); 


    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 esc_url( $url );
        }
    } else {
        return $redirect_to;
    }
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

The global $currentuser is unavailable during this filter request as stated here

But the $user is available during the request from function parameters, so simply these should work

function my_login_redirect( $redirect_to, $request, $user ) {
 $username = $user->user_login;
 $url = home_url( "/connections/$username/profile/edit/group/1/" ); 
 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    if ( in_array( 'administrator', $user->roles ) ) {
       return $redirect_to;
    } else {
        return esc_url( $url );
    }
 } else {
    return $redirect_to;
 }
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM