Store user’s registration date as meta

I am trying to modify the following code to register it as a meta field in the user’s profile to have registration date as a meta field. This would allow me to reach the information easily and integrate with other plugins that can fetch user meta fields.

One important use of this case is when you can pass meta fields along user’s information to external services or e-marketing systems.

Read More

This is the original code I am also using which I am trying to adapt. The following code is used to store last login as a meta field:

        add_action('wp_login', 'set_user_last_login');

        //function for setting the last login

        function set_user_last_login($login) {
           $user = get_userdatabylogin($login);

           //add or update the last login value for logged in user
           update_usermeta( $user->ID, 'last_login', current_time('mysql') );
        }

I’ve tried using this to store the registration date in a meta field but it doesn’t seem to work:

//Updating User Meta with registration date
add_action( 'user_register', 'set_user_registration_date' );             
//function for setting the registration date
$users = get_users();
$udata = get_userdata( $user->ID );
$registered = $udata->user_registered;

function set_user_registration_date($registered) {
$user = get_user_by($registered);

//add or update the last login value for logged in user
update_usermeta( $registered->ID, 'registration_date', $registered->user_registered );
}

I am not a PHP coder. I try to find my way, but I don’t seem to be able to get this.

I would appreciate any help or if someone has a better way to achieve this?

Thanks!

Related posts

1 comment

  1. In your case $registered is the user id not the user object so it does not work. Please use the code as given below.

    add_action( 'user_register', 'set_user_registration_date', 10, 1 );
    
    function set_user_registration_date( $user_id ) {
        $user = get_userdata ( $user_id );
    
        // Update the registration meta data
        update_user_meta ( $user_id, 'registration_date', $user->user_registered );
    }
    

Comments are closed.