1

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.

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!

4
  • Saving the same information in two different places is not usually a good idea, and is a good to way to cause problems. In this case you are resaving a value saved by the Core. Plugins and themes will expect that value in to be where the Core saved it, not where you resaved it. You are not gaining anything with this, you are just muddying the waters.
    – s_ha_dum
    Commented Nov 30, 2013 at 16:31
  • @s_ha_dum Thanks for your comment. I am quite new to Wordpress meta fields and wasn't aware that there is already a meta-field for this :). Thanks again!
    – Dr.Hariri
    Commented Dec 8, 2013 at 17:46
  • There isn't a meta field for "last login"-- you will have to add that-- but the registration date is stored in the $wpdb->users table, which is what your second block of code is re-saving.
    – s_ha_dum
    Commented Dec 8, 2013 at 18:44
  • @s_ha_dum Thanks! For a non-coder like me, it's much easier to have a meta-field especially when certain plugins can interact directly with meta-fields and not allow you to do some querying :)
    – Dr.Hariri
    Commented Dec 9, 2013 at 20:54

1 Answer 1

2

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 );
}
1
  • Thank you so much! this worked :). Sorry I guess because I am new I can't vote up your answer yet!
    – Dr.Hariri
    Commented Nov 30, 2013 at 11:38

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.