0

I'm quite new here so I hope you'll find my question documented enough. I've been digging into this for a while now, and haven't found satisfactory solutions, despites some looked relevant!

Basically, I do want to do something once a user profile has been updated.

And more specifically, I want do do something is the updated user has a given role.

In "my books", I should use the profile_update hook, and this work, but only to a certain extend. Here is my code

add_action( 'profile_update', 'my_custom_function', 10, 2 );

function my_custom_function( $user_id ) {

    //get data from the updated user
    $user_info = get_userdata( $user_id );
          
                  
                 
    //check if user has the role XXX
    if ( in_array( 'my_custom_role', (array) $user_info->roles ) ) {
        //do something

    }else {
        //do something else
    }

}

Edit - the issue is not above

Actually this code above is fine, per se. If you bump in a situation where the above code do not have the expected behaviour, you may try changing for priority to 25, or even 999 for debug purpose.

add_action( 'profile_update', 'my_custom_function', 999, 2 );

In my case, I did not have met the expected behaviour, but the issue is, again, not in the code above


Edit - How (and where from) do you change the role ?

This question has been asked in the comments, and is very important.

My first answer was "I do change the role from wp dashboard user profile edit page".

But this is only partially right.

It is right because I do edit my user from the wp-dashboard user profile edit page, but then what :

  • if I do change the role checking a role in the native wp-role checkbox of the user profile, then my code is working as expected !
  • but what I did was actually changing the user "membership level" (within paid membership pro plugin realm), which itself change the user role.

So yes on profile update, the role has changed, but in a certain manner (don't ask me which one, I do not know, yet)


Edit - a note on paid membership pro setting

I do have set paid membership pro in a way that it creates and sync a given wp-user role to given membership level.

And this worked flawlessly.

What was I doing : changing the user membership level (within paid membership pro plugin realm) from the wp-dashboard user edit page.


What happend to me and why have I open this topic ?

(this if from the original question with minor clarification edits)

I'm thus indeed editing the user from wordpress admin user edit page and, really what seems weird, it that with the above code the behaviour is :

  • save user profil without role change --> expected behaviour
  • save user profile with role change --> not expected behaviour

This also means that if I change the role, save, then save again, the second time I do meet the exepected behaviour ! But in the profil page the change apears righ after the first save (obviously, otherwise I would look for the issue elsewhere)

After some reading I got the idea that it may be because the change of the user role is not written down in the databse yet, when my function is run, but maybe it is something diferent ? (how to be sure ?)

I thus have looked for a way to delay my function. I've found a few post, for instance this one, or this one, but none of them (and many other try) did the job.

Indeed, I do only have/need one function so the first one is not applicable as such, and the second one cause that my_custom_function do not get the $user_id it requires to do something.

I feel dumb not finding a way to solve that out, and very surpised it hasn't been addressed before.

I hope I haven't missed something that was just under my nose, and be pleased if one can direct me with that :)

Regards


Edit - $user_info is not empty and contains

object(WP_User)#21062 (8) {
  ["data"]=>
  object(stdClass)#21063 (10) {
    ["ID"]=>
    string(2) "15"
    ["user_login"]=>
    string(9) "MyUserLogin"
    ["user_pass"]=>
    string(34) "XXXXXXXXXXX"
    ["user_nicename"]=>
    string(9) "MyUserNicename"
    ["user_email"]=>
    string(19) "[email protected]"
    ["user_url"]=>
    string(0) ""
    ["user_registered"]=>
    string(19) "2022-02-04 14:04:51"
    ["user_activation_key"]=>
    string(0) ""
    ["user_status"]=>
    string(1) "0"
    ["display_name"]=>
    string(5) "my name"
  }
  ["ID"]=>
  int(15)
  ["caps"]=>
  array(1) {
    ["my_custom_role"]=>
    bool(true)
  }
  ["cap_key"]=>
  string(22) "wp_XXXX_capabilities"
  ["roles"]=>
  array(1) {
    [0]=>
    string(12) "my_custom_role"
  }
  ["allcaps"]=>
  array(1) {
    ["my_custom_role"]=>
    bool(true)
  }
  ["filter"]=>
  NULL
  ["site_id":"WP_User":private]=>
  int(1)
}

Edit - comments

thank's for your many usefull comments, I'm deleting some of mine now answererd in the edits above, it'll be easier to read ;)


Edit - conclusion and follow-up question

The issus I encountered is most probably related to the way paid membership pro change the user role.

I stil can't really understand why I couldn't grab the new role on profile update, and still think it's a timing question. In the end the role has indeed been change, so I should be able to grab it.


0

1 Answer 1

0

Hooks fire in order so you can lower the priority to make it run after a previous one. However, if it is a different hook altogether you will have to see in which order they run to see which one you need to use.

add_action( 'profile_update', 'my_custom_function', 15, 2 );

function my_custom_function( $user_id ) {

    //get data from the updated user
    $user_info = get_userdata( $user_id );
          
                  
                 
    //check if user has the role XXX
    if ( in_array( 'my_custom_role', (array) $user_info->roles ) ) {
        //do something

    }else {
        //do something else
    }

}
8
  • This could potentially be a solution. We need to see how the user is updated to know if order of operations is the problem.
    – jdm2112
    Commented Feb 4, 2022 at 17:10
  • If I had to guess, the user(s) don't actually have that role. Using a simple check of administrator or not the conditional fires correctly. Toss a simple die("admin"); die('Not admin') in there for true/false and change your name for an admin or company name and see if it kills the page. if ( in_array( 'administrator', (array) $user_info->roles ) ) {
    – Bazdin
    Commented Feb 4, 2022 at 18:03
  • So as previously mentioned we would need to see additional code if its not working because the block of code works fine.
    – Bazdin
    Commented Feb 4, 2022 at 18:04
  • @jbonlinea dump the value for $user_info to verify your condition. Learning to use tools such as error_log() and var_dump() are critical to solving PHP issues. In this case, you would want to do something like this: error_log( print_r( $user_info->roles, true ) ); to print the object returned by get_userdata().
    – jdm2112
    Commented Feb 4, 2022 at 18:19
  • How are you updating the user? Because testing it via the admin changing anything or nothing and clicking on Update User fires the function correctly to determine the role.
    – Bazdin
    Commented Feb 4, 2022 at 18:40

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.