When you need to update user profiles with code in WordPress, there are two hooks available:

And there’s a subtle difference between the two. If you don’t use the proper hook for the task at hand, then you may not end up with the result you’re expecting.

Update User Profiles in WordPress

The idea for this post is coming based on some work I’ve done on a recent project, and I thought it’d be worth sharing how the two hooks work and when one is preferable over the other (or when you may want to use both).

Update User Profiles in WordPress

To be clear, both of these hooks provide a way for us to manage doing some work whenever a user profile is updated.

Perhaps this means manipulating something that’s already stored; perhaps it means adding, updating, or deleting metadata that we’ve created ourselves.

Whatever the case, it’s important to distinguish between the two hooks because if you’re expecting a particular type of functionality and it appears that it’s not working, it may be that you’re using the wrong hook.

The Difference Between Hooks

Before looking at some code, note the following as it relates to the personal_options_update hook:

This hook only triggers when a user is viewing their own profile page. If you want to apply your hook to ALL profile pages (including users other than the current one), then you also need to use the edit_user_profile_update hook.

The key to this hook is understanding that it fires when a user is manipulating their profile.

So if you’re an administrator of a site, and you want to change custom metadata related to a given user, this is not the hook to use. Instead, you’re looking for edit_user_profile_update:

This hook only triggers when a user is viewing another user’s profile page (not their own). If you want to apply your hook to ALL profile pages (including the current user) then you also need to use the personal_options_update hook.

But this still leaves us with a question:

What if we want to introduce some functionality that allows us to act as an administrator and update some in both our profile and our users’ profiles?

In this case, it’s a matter of using both hooks and also checking for the roles and capabilities of the user who is logged into the backend of WordPress.

An Example Function

There’s no single way to show how to structure a function for something like this because your requirements may vary, but it’s worth showing some example code to at least show how to take advantage of both of these hooks.

So for the following example, assume the following:

  • The function is part of a plugin being used by an administrator,
  • The user profile options are part of the administrator’s profile as well as all user profiles,
  • The administrator may need to update his/her profile as well as other user profiles.

With that in place, check out the following code:

Though there’s not that much code, it should be relatively easy to follow. To be complete, here’s exactly what it’s doing:

  1. The function is defined and attached to both of the hooks covered earlier in this article.
  2. If the current user is not an administrator, then the function exits and returns control to WordPress.
  3. Otherwise, the function continues with whatever functionality is defined.

As you can see, no other functionality is defined above since that’s completely up to you. If you’re looking for an example, then consider updating some type of user metadata where you may want to toggle an option or update a value based on what the user has provided.

As mentioned, this will need tweaking based on your particular use case, but the bottom line is to understand the difference in the two hooks and to make sure you’re properly checking permissions before doing any actual work on the user’s profile.

Category:
Articles
Tags:

Join the conversation! 2 Comments

  1. Very helpful article…Please can you guide me to change the admin username Admin to something else. Is it possible to change the Admin username ?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.