WordPress edit_user_profile_update update secondary role

The question:

I am trying to save secondary role field in user-edit.php which is independent of WP’s main roles. I had no problem with saving other custom fields that are unique but for roles (wp_capabilities), it looks like it first saves my roles (I set sleep(10) to check in database in the process) and at the end of request, WP saves main role field which overwrites my previously saved role.

Is there any way to order events somehow so my function executes at the very end of request?

Here’s what I’ve got so far:


add_action( 'edit_user_profile',        array( $this, 'test_profile_form'));
add_action( 'edit_user_profile_update', array( $this, 'test_save_profile_form' ));


function hook_save_profile_form($user_id) {
        if(!current_user_can("edit_user",$user_id)) {
            return false;
        $user = new WP_User($user_id);

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I have finally found solution and the right hook for this: profile_update is called at the end of wp_insert_user in user.php which is called from edit_user.

wp_insert_user ending:

if ( $update )
        do_action('profile_update', $user_id, $old_user_data);
        do_action('user_register', $user_id);

    return $user_id;

Method 2

I was using the personal_options_update and edit_user_profile_update hooks, but the database information was being overwritten. Those hooks work when editing the current user, but when an administrator edits another user’s profile, the information is not persisted. I switched to using the profile_update hook and the updates were persisted when editing any user, so this appears to be the best hook to use in cases where you want to edit all user profiles and not just the current user.

Method 3

You can use the third argument of add_action to set the filter priority. The functions default value is 10, so something >10 should work.

Reference: http://codex.wordpress.org/Function_Reference/add_action

Method 4

Using profile_update hook I can solve this problem.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment