Is it possible to incorporate username in a login redirect?

I’m using WP to work as the CMS for a website which will be provided to users of a number of different products. I’ve successfully created a function which will redirect users based on their role:

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
function my_login_redirect( $redirect_to, $request, $user ) {
if ( is_array( $user->roles ) ) {
    if ( in_array( 'administrator', $user->roles ) )
        return home_url( '/wp-admin/' );
    elseif ( in_array( 'editor', $user->roles ) )
        return home_url( '/wp-admin/' );
    elseif ( in_array( 'subscriber', $user->roles ) )
        return home_url();
    elseif ( in_array( 'map_user', $user->roles ) )
        return home_url( '/mapping/' );
    elseif ( in_array( 'news_subscriber', $user->roles ) )
        return home_url( '/category/newsletter/' );
    else
        return home_url();
}

Some users need to be redirected to a specific page which shares their username.

Read More

How can I retrieve the username and incorporate it into their redirect? So:

...
elseif ( in_array( 'map_user', $user->roles ) )
    return home_url( '/mapping/[username-based-page-goes-here]/' );
...

[I’m aware of Peter’s Redirect plugin, I’d rather make use of WordPress core functions to save on having to manually tailor each user’s access upon creation. I also make use of other plugins which seem to conflict with Peter’s.]

Related posts

Leave a Reply

1 comment

  1. $user->data should have what you want.

    // snip
    elseif ( in_array( 'map_user', $user->roles ) )
        return home_url( "/mapping/{$user->data->user_login}/" );
    // end snip
    

    Add if (!is_wp_error($user)) wp_die(var_dump($user)); to the top of your function to see what you have to work with. Caution: This will break things. (It throws a notice as well) It is for debugging only.