简体   繁体   中英

wordpress - track when user data updated - hook not working

On the front end of the site there is the facility to edit / update user details which is done via AJAX.

I want to track the last time user data is updated and have written a simple function for this. However this isn't working and I am not even sure if the function is being executed. I have tried using the following hooks but none work.

update_user_meta, updated_user_meta, profile_update

Can anyone see what I am doing wrong?

//add date / time to user meta data when details are updated

add_action( 'update_user_meta', 'updated_user_details' );

function updated_user_details(){

    $user_id = get_current_user_id();

    $datetime = date('Y-m-d H:i:s');

    update_user_meta( $user_id, 'updated', $datetime );

}

There are two things wrong with this code that I can see straight away.

Firstly, do_action is used to create actions, rather than hook into them. To hook into them, you'll need to use add_action ( See here )

Secondly, as far as I can see, wp_update_user isn't a valid action that you can hook into. I think what you're looking for is updated_{$meta_type}_meta ( See here )

So it would be something more like:

add_action( 'updated_user_meta', 'updated_user_details' );
function updated_user_details($meta_id, $object_id, $meta_key, $_meta_value){

     $user_id = get_current_user_id();

    //check to see if 'updated' field exists
    $updated= get_user_meta($user_id, 'updated', TRUE);

    //if yes update date /time
    $datetime = date('Y-m-d H:i:s');

    update_user_meta( $user_id, 'updated', $datetime );
}

Note: This hasn't been tested but should give you some idea and some refs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM