This is more of a theory question than a question with a direct answer.
I’ve been working with different actions for firing functions when a post is updated or deleted and when users are updated or deleted. For the actions, publish_post
and before_delete_post
for the posts and personal_options_update
, edit_user_profile_update
and delete_user
for the users.
With post update you get access to the values that are currently set and the values the new values at the same time to make any changes you see fit before anything happens. With user update you only have access to the new information and only after it’s already been set.
Is there a reason for this difference or is this just inconsistent?
…and don’t get me started on publish_{custom_post_type} vs before_delete_post.
2 Answers
It depends on what exact action you are using, and what the purpose of that action is.
For the update actions, for example, there should always be hooks that are triggered before doing the change (so that you may change or prevent it), and hooks that are triggered after the change has been completed (so that you can react to changes).
In your “user” case, if you want to compare the existing vs the changed user, you want to hook into the user_profile_update_errors
action. This action gives you an object that shows you what the intended changes are, and whether there’s already an existing user. In that case, you can retrieve the existing user through its ID and compare them both.
See: https://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors