2

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 }

2

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 );
| improve this answer | |
  • Thanks Nathan. That's very helpful! It's not exactly what I was wanting, as I was hoping to be able to process the whole form at once. But it does do what I was looking for 1 field at a time. – dwlorimer Feb 2 '17 at 15:18
  • I tried this and it seems to fire whether on update OR create...is there a way to make sure this only fires on update and not create? if( true !== $update ) return; does not seem to work... – Stephen Apr 23 '18 at 18:01
  • It should have been returning $meta. It does now, thanks. – Nathan Johnson Apr 23 '18 at 18:04
  • Thanks! I figured it out and actually just modified it to be if($update) {//do stuff} return $meta; – Stephen Apr 23 '18 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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