Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wonder how to redirect a user after login to the page called by his first name (name used for registration).

Example: Username or first name is John, after login he should be redirected to website.com/john page.

I tried to combine these 2 examples but without luck.

add_filter( 'login_redirect', 'ckc_login_redirect' ); 
function ckc_login_redirect() {
    // Change this to the url to Updates page.
    return home_url( '/Updates' ); 
}

&

global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";

Please help PHP newbie :)

share|improve this question
    
Keep in mind there probably is no chance of having only unique first names. In regard to your example, what if there is a second John? –  ialocin Feb 22 at 11:38

2 Answers 2

up vote 1 down vote accepted

Try the following code in your functions.php:

function my_login_redirect( $redirect_to, $url, $user ) {

    global $user;
    if( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if( in_array( "administrator", $user->roles ) ) {
            // redirect them to the default place
            $redirect_to = admin_url();
        } else {
            $redirect_to = site_url( '/' . $user->user_login ); // Or $user->first_name
        }
    }

    return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
share|improve this answer
    
That code works only if user is "John". What I want to accomplish is to have it work for all users. Which means, if user is John, redirect to mysite.com/john page, if user is Mary, redirect to mysite.com/mary page etc.. It should be automatic, and I feel that solution is near, just cannot figure it out myself. Thank you! –  user48008 Feb 22 at 13:27
    
Just updated the code. It's kinda weird but should work. –  1fixdotio Feb 22 at 17:36
    
It works it works it works !!! :) Thank you very much! Just one small edit if you have time. I am not sure how to do it within this function, this > IF user is admin dont redirect anywhere or even better, redirect to admin panel –  user48008 Feb 23 at 1:33
    
Just updated the code again. –  1fixdotio Feb 23 at 5:25
    
Thank you. I don't see way to contact u, so I posted a comment on your site. Please check it out. –  user48008 Feb 23 at 14:32

Try this -

function wpse_login_redirect( $redirect_to, $url, $user ) {
    if ( ! is_wp_error( $user ) && !empty( $user->first_name ) ) {
        $redirect_to = site_url( sanitize_title_with_dashes($user->first_name) );
    }
    return $redirect_to;
}
add_filter( 'login_redirect', 'wpse_login_redirect', 10, 3 );

But, as like @ialocin said, first names are not unique, so not sure this would be a proper approach in some case. However, other way it's also seems to OK as you are doing this only for the logged-in user, so both john should see their own profile data on same /john page.

share|improve this answer
    
This code looks ok visualy, but it dont works, I checked on 2 sites and it shows errors, please check whats the issue. Yes, thats right, there should be Username instead of First name. Thanks for your effort. –  user48008 Feb 22 at 14:29
    
What is the error message you are getting ? –  Shazzad Feb 22 at 14:32
    
( ! ) SCREAM: Error suppression ignored for ( ! ) Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'my_login_redirect' not found or invalid function name in C:\wamp\www\test\wp-includes\plugin.php on line 199 Call Stack # Time Memory Function Location 1 0.0035 1089024 {main}( ) ..\wp-login.php:0 2 0.2063 26967312 apply_filters( ) ..\wp-login.php:765 3 0.2064 26969096 call_user_func_array ( ) ..\plugin.php:199 Test –  user48008 Feb 22 at 14:37
    
Try with the updated code. –  Shazzad Feb 22 at 14:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.