Support » Plugin: WP Webhooks » User Updated in the background

  • Resolved vargaszabolcs

    (@vargaszabolcs)


    Hi!

    Is there a way to make the ‘Send data on user update’ webhook work, when the user is updated via a plugin with code “in the background” (for eg. changing the user role), and not only when the user is manually updated?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ironikus

    (@ironikus)

    Hey @vargaszabolcs – thank you very much for your message, as well as for using our plugin! 🙂

    To answer your question: This should be the case by default since our plugin uses the WordPress standards to fire the trigger.
    To be explicit, our plugin first the trigger on the profile_update hook (https://developer.wordpress.org/reference/hooks/profile_update/), which is called once a user is created or updated.

    In case the plugin code you use follows the WordPress standards by using either the wp_update_user function (https://developer.wordpress.org/reference/functions/wp_update_user/) for updating a user or the wp_insert_user function (https://developer.wordpress.org/reference/functions/wp_insert_user/) for creating a user, this should work straight away. 🙂

    Pro tip: You can find the same information within the web view of our plugin at https://wp-webhooks.com/?wpwh=wp-webhooks-pro&wpwhtb=send-data
    When you click on the Send Data On User Update webhook trigger, you will find a description tab, which contains the above-mentioned info as well. This counts for each webhook trigger.

    If you require anything else, feel free to let me know. 🙂

    Thread Starter vargaszabolcs

    (@vargaszabolcs)

    Hi!

    Thank you for the answer! The plugin in question uptades the user role with the ‘set_role’ method, so it not applies to this. I don’t think however that the plugin developers do not follow WordPress standards, because the plugin is the “AutomateWoo” plugin, which is managed by Automattic.

    Plugin Author Ironikus

    (@ironikus)

    Hey @vargaszabolcs – Thank you for your answer. You are correct, Automattic is following the standards, but in this case, even WordPress is not following the correct approach since it updates the roles using directly the user meta instead of the wp_update_user function.

    Unfortunately, this is kind of a habit in the community 🙂

    To make it compatible at this point, simply add the following snippet to your functions.php file:

    add_action( 'set_user_role', 'wp_webhooks_trigger_as_user_update', 10, 1 );
    function wp_webhooks_trigger_as_user_update( $user_id ){
    
    	if( empty( $user_id ) ){
    		return;
    	}
    
    	$old_user_data = get_userdata( $user_id );
    
    	//Fire the profile update hook manually
    	do_action( 'profile_update', $user_id, $old_user_data );
    
    }

    All it does is to provide a fully functional call of the original profile_update hook call once the role was set using the WP_User::set_role() function.
    Hope this helps. If you require any further questions, feel free to let me know at any time. 🙂

    Thread Starter vargaszabolcs

    (@vargaszabolcs)

    Hi!

    I see, yes, it is a bit strange.
    Nevertheless, the snippet is working great, so my problem is solved! Thank you for your help!

    Plugin Author Ironikus

    (@ironikus)

    Hey @vargaszabolcs – thank you for the feedback, happy to read it solved your problem. 🙂
    In case you ever have any questions, feel free to reach out again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘User Updated in the background’ is closed to new replies.