Site icon Hip-Hop Website Design and Development

Delay action unltil the untill previous (hoocked) action is completed

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.

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
    }

}

What happends is :

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

Regarding the many comments (thank’s guys)

$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) "my@user.email"
    ["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)
}

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

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 after the first save (obviously, otherwise I would look for the issue elsewhere)