I am trying to detect when a user changes their name.
I have tried using the profile_update
hook:
add_action( 'profile_update', 'my_profile_update', 10, 2 );
function my_profile_update( $user_id, $old_user_data ) {
https://codex.wordpress.org/Plugin_API/Action_Reference/profile_update
I can access the $_POST
data to get the new name fields, but $old_user_data
does not contain the name fields, and because this fires after the data get saved, the old names fields are not in the database either.
I looked through user.php
, and the best I see is filters for each name field, for example:
$meta['first_name'] = apply_filters( 'pre_user_first_name', $first_name );
I would prefer something that gives me access to all the data, though, instead of checking each name individually.
Is there a hook that fires after the form is submitted but before the data is saved?
I’m wanting to do something like this:
if ($new_first_name != $old_first_name) { //store old_first_name }
1 Answer
The hook you’re looking for is insert_user_meta
.
add_filter( 'insert_user_meta', function( $meta, $user, $update ) {
if( true !== $update ) return $meta;
$old_meta = get_user_meta( $user->ID );
if( $old_meta[ 'first_name' ] !== $meta[ 'first_name' ] ) {
//* Do something
}
return $meta;
}, 10, 3 );