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 have a simple requirement, after user login set the login user session and then redirect to another page.

Yes, I found some similar page from here is-there-a-hook-that-runs-after-a-user-logs-in

As the post said, what I need to do is like this:

function set_blog_session(){
    $_SESSION['blogger'] = get_current_user_id();
    wp_redirect("http://www.google.com");//just for test
}
add_action('wp_login', 'set_blog_session');

But I got nothing.

First, get_current-user_id() always returns 0.
Second, no redirect at all.

Whats going wrong?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You should use exit after using the wp_redirect():

From the Codex:

wp_redirect() does not exit automatically and should almost always be followed by exit.


For your hook, you could pass a parameter in your callback function to get the user login and then use get_user_by() to get the ID.

function set_blog_session($user_login){
    $user_obj = get_user_by('login', $user_login );
    $_SESSION['blogger'] = $user_obj->ID;

    wp_redirect("http://www.google.com");
    exit;
}
add_action('wp_login', 'set_blog_session');
share|improve this answer
    
thanks man, yes, need to be add exit. And what's wrong with my first problem, why get_current_user_id() got 0? –  diligent Jul 12 '13 at 8:13
    
I updated my answer –  RRikesh Jul 12 '13 at 8:17
    
it works man, but I wonder, why use get_current_user_id() not working, because I have been logon, so it should be get the current user id ? Thanks –  diligent Jul 12 '13 at 8:28
    
I don't know why get_current_user_id() doesn't work, maybe someone else will help us understand that point. –  RRikesh Jul 12 '13 at 9:19

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.