• can someone help me, i want to access the value after trigger create_user

    i have url in WP webhooks like this.

    https://localhost/api/call.php

    once new user register, i want to put his email in txt file.

    call.php
    file

    $data = json_decode( file_get_contents('php://input'), true);
    $email = $data->data->email;
    $file = "emails.txt";
    file_put_contents($file, $email);

    i think $data->data->email is null, how to get data, i tried also foreach but not working.. any help?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter r123ze

    (@r123ze)

    i forget, the file txt is created but always empty.

    Plugin Contributor Ironikus

    (@ironikus)

    Hey @r123ze – thank you for reaching out. ??

    Since the code you provided above is completely independent of our plugin, I will provide you an example based on your requirements.
    Since you want to fire the code after the create_user action was fired, I suggest you using the do_action argument. This allows you to fire custom code directly after the user was created.
    Here’s what you need to to:

    1. Set the do_action argument and as a value, please include add_txt_file
    2. Once that’s done, please include the following code into your functions.php file:
      add_action( 'add_txt_file', 'add_custom_txt_file', 10, 2 );
      function add_custom_txt_file( $user_data, $user_id ){
      
      	//Get the response body from the incoming request
      	$response_body = WPWHPRO()->helpers->get_response_body();
      
      	//Grab the email from the incoming request
      	$email = WPWHPRO()->helpers->validate_request_value( $response_body['content'], 'user_email' );
      
      	//Fire your custom logic
      	$file = "emails.txt";
      	file_put_contents( $file, $email );
      }

    This code will grab the email from the first layer of the payload, that is sent over to the webhook endpoint, and adds it to the file.
    Please note: In case the key in which you store the email is different than user_email, please adjust it within the validate_request_value() function.

    I hope this helps you so far. Feel free to let me know in case you have further questions. ??

    • This reply was modified 4 years, 3 months ago by Ironikus.
    Thread Starter r123ze

    (@r123ze)

    Hi @ironikus,

    thanks for your quick reply.

    The problem is that i don’t want to do it in wordpress, but in another external url wich i put in wp webhooks plugin.

    I think wpwh plugin fired once registered because it created the emails.txt file, but i don’t get the email to store it in txt file.

    When tried my first code above, i find always the email.txt is empty. But when I change it to this.

    $data =  file_get_contents('php://input');
    $file = "emails.txt";
    file_put_contents($file, $data);

    I get all data in file like this format, but I want only to get email or username of the registered user..

    {"data":{"ID":"36","user_login":"r123ze","user_pass":"$P$BXnUKsyaZXj3tVBgZ00Wbb9E6HHfK0.","user_nicename":"r123ze","user_email":"[email protected]","user_url":"","user_registered":"2020-11-12 08:48:04",....

    If removed json_decode, the create_user fired and create file txt with this data, but if i put json_decode, it does not create txt file and not working.. can you tell me how to fix this please?

    Thanks so much! ??

    Thread Starter r123ze

    (@r123ze)

    According to outgoing value, the json format is:

    Array
    (
        [data] => Array
            (
                [ID] => 1
                [user_login] => admin
                [user_pass] => $P$BVbptZxEcZV2xDLyYeN.O4ZeG8225d.
                [user_nicename] => admin
                [user_email] => [email protected]
                [user_url] => 
                [user_registered] => 2018-11-06 14:19:18
                [user_activation_key] => 
                [user_status] => 0
                [display_name] => admin
            )
    ...

    So, if i want to access it, do i need to use oop or Multidimensional php array?

    $json->data->user_email;
    or
    $json['data']['user_email']

    i want to access it like object oop $json->data->user_email;

    Plugin Contributor Ironikus

    (@ironikus)

    Hey @r123ze

    With the example given, you format the decoded JSON to be an array (that’s what the second parameter is for. Please see https://www.php.net/manual/de/function.json-decode.php for that.

    If you use $data = json_decode( file_get_contents('php://input'), true);, then you need to access the data using $json['data']['user_email']

    If you access it via json_decode( file_get_contents('php://input'));, then you can access it via $json->data->user_email;

    • This reply was modified 4 years, 3 months ago by Ironikus.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘access object json data post after create_user trigger’ is closed to new replies.