• Resolved markconan

    (@markconan)


    Hi again Ironikus,

    I just want to ask if what Action Hook Should I use to get the latest data when a user updates a profile.

    I know you already have the Send Data On User Update option on the plugin.
    But I want something custom so I used the profile_update action hook of wordpress.
    But it doesn’t get the latest data of the user, It seems that it gets the previous data before it was updated.

    add_action( 'profile_update', 'my_profile_update', 10, 2 );
    	function my_profile_update( $user_id, $old_user_data ) {
              $custom_data = array(
                 //custom data here
              );
              do_action('wp_webhooks_send_to_webhook', $custom_data);
    	}

    Hope you can help me with this one.
    Thanks in advance!

    • This topic was modified 4 years, 2 months ago by markconan.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ironikus

    (@ironikus)

    Hey @markconan – thank you a lot for reaching out!

    I assume you add for your current placeholder //custom data here something like that: get_post_meta(), correct?
    In most cases, this will only send over the default fields, because most of the external plugins are badly coded if it comes to this aspect.

    To explain that: Most of the plugins that work with custom meta, including ACF, adds the meta values within a custom function AFTER the post is saved.
    This means that during the time where WordPress triggers the profile_update hook, the custom post meta is not even saved since the external plugins use hooks like wp or init to save their data, using POST or GET parameters.

    To fix that, we created a logic, that is able to delay the sending of the webhook to send it right before the shutdown of PHP.

    What this means regarding your code is that you need to pre-buffer the incoming request you are going to send. For that, you need to create an additional function, where you add your post_delay function.
    Here’s a functional code, based on your example:

    add_action( 'profile_update', 'my_profile_update_delay', 10, 2 );
    function my_profile_update_delay(){
    	WPWHPRO()->delay->add_post_delayed_trigger( array( $this, 'my_profile_update' ), func_get_args() );
    }
    function my_profile_update( $user_id, $old_user_data ) {
    		$custom_data = array(
    			//custom data here
    		);
    		do_action('wp_webhooks_send_to_webhook', $custom_data);
    }

    If you have any further questions, feel free to reach out at any time.

    Thread Starter markconan

    (@markconan)

    Thanks for the response. 🙂

    I did add the delay function:

    add_action( 'profile_update', 'my_profile_update_delay', 10, 2 );
    function my_profile_update_delay(){
    	WPWHPRO()->delay->add_post_delayed_trigger( array( $this, 'my_profile_update' ), func_get_args() );
    }
    function my_profile_update( $user_id, $old_user_data ) {
    		$custom_data = array(
    			//custom data here
    		);
    		do_action('wp_webhooks_send_to_webhook', $custom_data);
    }

    Whenever I update a user from a form, it returns
    mywebsite.com/wp-admin/admin-ajax.php 500
    when “$this” is present, so I replaced “$this” to “my_profile_update_delay”.
    now, it doesnt return any error but it doesnt push any webhook either.

    seeking for further help.
    thanks!

    Plugin Author Ironikus

    (@ironikus)

    Hey @markconan – thank you for your feedback!
    I think that’s my mistake since I assumed you are going to use this functionality within a class.
    If you simply use it within your functions.php file, please remove the $this completely, as well as the whole array.

    Here is the updated version:

    add_action( 'profile_update', 'my_profile_update_delay', 10, 2 );
    function my_profile_update_delay(){
    	WPWHPRO()->delay->add_post_delayed_trigger( 'my_profile_update', func_get_args() );
    }
    function my_profile_update( $user_id, $old_user_data ) {
    		$custom_data = array(
    			//custom data here
    		);
    		do_action('wp_webhooks_send_to_webhook', $custom_data);
    }
    Thread Starter markconan

    (@markconan)

    Hi Ironikus,

    Yes, just editing doing codes on functions.php.
    This Works perfectly, Just what I needed!

    Thank You Again. 🙂

    This is really interesting…
    @markconan can you give an example of how you added your custom data please?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘What Action Hook to use For Custom Action’ is closed to new replies.