Admin user edit – Grab newly submitted user meta immediately after update/submit

I am trying push user update fields to an api via user profile edit or admin user edit.

I have successfully allowed user to update their own profile and have it also update the remote api with same (new) update data.

I have had less success getting the admin side to work correctly.

if  ( is_admin () ) {
    add_action ( 'profile_update', 'myplugin_admin_update', 10, 2 );

    //add_action ( 'edit_user_profile_update', 'myplugin_admin_update', 10, 2 );
    // ^ Fires before form submit. Don't think I can use it.
...
}

 function myplugin_admin_update ($user_id, $old_user_data) {
 $user_meta                 = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );
...
}

I then basically send this meta off to another routine to be processed and sent. That part works fine.

The problem is the data is ‘old’. Oddly, the data I pull from DB using hook profile_update is not the submitted/new data but the old data – contrary to description in api codex. I understand the hook delivers $old_data but I am not interested in that – just when it fires. I only need a hook that fires immediately after the admin updates a user. Not before. I need the newly submitted data.

profile_update This hook allows you to access data for a user immediately after
their database information is updated

What am I doing wrong? I get ‘old data’ when i query DB on this hook.

EDIT:

I was only testing certain meta-fields as it turned out. I updated ‘First Name’ and it was the new value being posted to API. Other fields show old data sent to api as mentioned above.

I didn’t mention in original post that these meta fields reading as old data are ‘WP Member’ plugin fields (added via plugin). They are still stored in usermeta in DB, though.

If I click ‘Update’ twice on the user I am able to update API all with new values but, this seems a feeble work-around.

So, now the question is: Why are only some meta values reading as new values?

1 Answer
1

I suspect that the problem lies in the priority of the profile_update action.

You are using priority 10, which is also the default priority. The WP-Members plugin updates custom user meta fields using this same hook, also at the default priority. So, the plugin’s update is likely happening after your process. (This is probably why you are seeing “new” data when hitting update twice as well. The “new” data is no longer new at that point – It’s now the data that is saved for those fields.)

If you change the priority so that your action comes later you should see it updating (provided there aren’t issues with the rest of your function). Set it at 11 or higher for it to come after WP-Members updates data:

add_action ( 'profile_update', 'myplugin_admin_update', 11, 2 );

Leave a Comment