0

I am experimenting with showing selected data from an API response in a form submission confirmation in wordpress. This will be with POST in reality but have used GET for testing. I have the following which works for showing a single value from the JSON response ('id' in this case). I want to know how to write the print_r instruction to print 2 or 3 of the values (by name rather than position). For example show the 'id' and the 'login' values. I've used github api for demo.

add_filter( 'gform_confirmation_4', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
$response = wp_remote_get( 'https://api.github.com/users/someuser' );

if ( is_wp_error( $response ) ) {
echo 'An error happened';
} else {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
}

$confirmation .= print_r( $data->id, true );
return $confirmation;

}

I was assuming something like

$confirmation .= print nl2br(print_r($data->id, true), ($data->login, true));

but finding how to print_r multiple specific values with this '->' format is something I don't see in the manuals or forums.

edindubai
  • 151
  • 1
  • 8
  • 1
    The `->` is irrelevant. Your issue is trying to print multiple variables using a single print_r statement. This would be an issue for plain variables as well as object properties. You either just use multiple print_r statements concatenated together (e.g. `$confirmation .= print_r( $data->id, true )." ".print_r($data->login, true);`, or you could perhaps switch to using var_dump (see [here](https://stackoverflow.com/questions/10441757/printing-more-than-one-array-using-print-r-or-any-other-function-in-php) for example). – ADyson Jan 02 '20 at 10:25
  • That concatenation approach solves it and adding
    between the " " generates the new line in this example. Brilliant.
    – edindubai Jan 02 '20 at 10:58

1 Answers1

0

The -> is irrelevant. Your issue is trying to print multiple variables using a single print_r statement. This would be an issue for plain variables as well as object properties.

You either just use multiple print_r statements concatenated together (e.g. $confirmation .= print_r( $data->id, true )." ".print_r($data->login, true);, or you could perhaps switch to using var_dump (see here for example).

ADyson
  • 57,178
  • 14
  • 51
  • 63