add_user_meta and update_user_meta working only sporadically

admin

Administrator
Staff member
I'm trying to display something to my wordpress site users depending on the number of times they have logged in. I've tried to accomplish this using user_meta and the wp_login hook.

Code:
add_action( 'wp_login', 'survey_login' );
function survey_login() {
    global $current_user;
    get_currentuserinfo();
    $login_count = get_user_meta($current_user->ID, 'login_count', true);

    if($login_count == "") { $login_count = 0; }

    update_user_meta($current_user->ID, "login_count", $login_count++ );

    if($login_count >= 5) {
        $_SESSION['csm_survey_login'] = true;
    }
}

This seems like it should work, but for some reason the user_meta key only gets added/updated about 1/20th of the time. I've been able to find no pattern to how or why.

I've tried simplifying the function to simply

Code:
add_user_meta($current_user->ID, 'login_count', 1);

Or

Code:
update_user_meta($current_user->ID, 'login_count', 1);

Both are giving me the same trouble.

<strong>Anyone know why update_user_meta or wp_login may only work a fraction of the time?</strong>