0

I have begun to create a function to store last login date of user using time() function. Perfectly works!

Now I want to store in a serialize array a list of all user login date.

Question : Do I have to serialize before sending array, or deserialize array I receive?

Problem : For the first time of user is logged I get well one array with first timestamp. (D.B display)

first log in :
a:1:{i:0;i:1703863394;}
second log in :
a:2:{i:0;a:1:{i:0;i:1703863394;}i:1;i:1703863459;}
third log in :
a:2:{i:0;a:2:{i:0;a:1:{i:0;i:1703863394;}i:1;i:1703863459;}i:1;i:1703865295;}

For the second and all the following my function adds each time an array on top of the current one instead of inserting the value after it (php var_dump())

 **Existing last login :**
    array(1) { [0]=> string(23) "a:1:{i:0;i: 1703863394;}" } 
 **New array of last login :**
    array(2) { [0]=> string(23) "a:1:{i:0;i: 1703863394;}" [1]=> int(1703863459) }

I must get only one array with a list of timestamp with the new one at the end . Currently I get two arrays with more one timestamp stored. It is like my function creating a multidimensional array instead of insert new value at the end of existing array.

My function :

function user_last_login_list( $user_login, $user ) {
    $UserLoginDate = [];
    // If last_login user meta exist
    if(metadata_exists( 'user', $user->ID, 'last_login' )){
        //Get all login date if exist
        $ListLoginDate = get_user_meta($user->ID, 'last_login');
        
            foreach($ListLoginDate as $Timestamp){
                array_push($UserLoginDate,$Timestamp;
            }
        //Add the current day to array of login date 
        array_push($UserLoginDate, time()) ;
    }else{ // If it's the first time user logs on
        $UserLoginDate[] = time() ;
    }
    // I tried to use true/false for previous value to check for the fourth parameter without change
    update_user_meta( $user->ID, 'last_login', $UserLoginDate );
}
add_action( 'wp_login', 'user_last_login_list', 10, 2 );

thanks for your help.

1 Answer 1

0

Try this, notice how I set the 3rd value of get_user_meta() to "true"

function user_last_login_list( $user_login, $user ) {
    // If last_login user meta exist
    if(metadata_exists( 'user', $user->ID, 'last_login' )){
        // Get all login dates as a single array
        $UserLoginDate = get_user_meta($user->ID, 'last_login', true);
    } else {
        // Initialize as an empty array if it's the first time user logs on
        $UserLoginDate = [];
    }

    // Add the current day to array of login dates
    $UserLoginDate[] = time();

    // Update user meta with the updated array
    update_user_meta( $user->ID, 'last_login', $UserLoginDate );
}
add_action( 'wp_login', 'user_last_login_list', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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