最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览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.


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.


Share Improve this question edited Feb 5, 2022 at 11:55 jbonlinea asked Feb 4, 2022 at 15:22 jbonlineajbonlinea 1012 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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
    }

}
发布评论

评论列表(0)

  1. 暂无评论