0

I am trying to redirect the user after login/signup to profile page, but I am not sure why is it not putting the user nickname in url.

$current_user = wp_get_current_user();

function aloginuser( $user_id ) {
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    wp_redirect( "/profile/$current_user->user_login" );
    exit;
}
add_action( 'user_register', 'aloginuser' );

It need to put site.com/profile/username

  • What happens if you add $current_user = wp_get_current_user(); after the wp_set_auth_cookie call? – czerspalace Feb 10 '16 at 22:53
  • I think you should use wp_login hook. And looks like $current_user is out of scope inside your function. – тнє Sufi Feb 11 '16 at 1:23
0

Try placing a header() function instead of wp_redirect, so

header("Location: mysite.com/profile/username");
  • Please explain why that would solve the problem. I cannot see any improvement over the existing code. – fuxia Feb 10 '16 at 22:22
  • Yes! How it will solve the problem? Please explain? – Jarvis Feb 10 '16 at 22:26
  • It will not add currently logged username in url – Jarvis Feb 10 '16 at 22:36
0

This can be done like this:

function redirect_user ($user_login, $user) {
    wp_redirect( "/profile/" .$user_login );// $user_login is $user->user_login
    exit;
}
add_action('wp_login', 'redirect_user', 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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