1

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:

Hooks:

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

Callbacks:

function hook_save_profile_form($user_id) {
        if(!current_user_can("edit_user",$user_id)) {
            return false;
        }
        $user = new WP_User($user_id);
        $user->add_role($_POST['secondary-role']);
        //debug
        sleep(10);
}
4
  • 1
    It has been awhile since I seriously looked into it, but I am pretty sure you can't assign a "secondary" role. One user; one role. One role will overwrite the other.
    – s_ha_dum
    Jan 28, 2014 at 14:28
  • 1
    It is indeed possible because there is add_role and set_role (set_role removes all current and sets the one provided). I used bbPress as an example, where wp_capabilities for each user look something like a:2:{s:13:"administrator";b:1;s:13:"bbp_keymaster";b:1;}
    – Nikola R.
    Jan 28, 2014 at 15:07
  • 2
    WP core can be configured to handle multiple roles for each user. Unfortunately the profile cannot handle that situation. I am not sure if it is possible to enable multiple roles in the profile via plugins - for display yes, for updating = don't know.
    – user42826
    Jan 28, 2014 at 15:07
  • Interesting... you are right. +1 to both of you.
    – s_ha_dum
    Jan 28, 2014 at 15:10

4 Answers 4

2

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);
    else
        do_action('user_register', $user_id);

    return $user_id;
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.

1
  • just want to confirm that using profile_update solved the same problem i was having... data was not persisting until i used profile_update instead of edit_user_profile_update
    – aequalsb
    Jun 22, 2023 at 3:34
0

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

2
  • I've tried that already but that can't help because user-edit.php calls edit_user($user_id) right below edit_user_profile_update hook...
    – Nikola R.
    Jan 28, 2014 at 16:12
  • 1
    Did you try another hook? profile_update located in wp-includes/user.php is called right after user data has been saved. I think this where you should put all update operations.
    – Jörn Lund
    Jan 28, 2014 at 16:46
0

Using profile_update hook I can solve this problem.

1
  • 2
    Please add a sufficient explanation how it would solve the problem - thanks. Sep 17, 2015 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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