Description
The gravityflowincomingwebhook_response_post_process filter allows the WP_REST_Response object to be modified after processing.
Parameters
Parameter | Type | Details |
---|---|---|
$response | WP_REST_Response | The current response |
$entry | Array | The current entry array. |
$data | Array | The request data. |
$request | WP_REST_Request | The entire request object. |
Examples
Adding an entry ID to the response body
1 2 3 4 5 6 7 8 9 10 | add_filter( 'gravityflowincomingwebhook_response_post_process' , 'jo_incoming_webhook_response_example' , 10, 4); function jo_incoming_webhook_response_example( $response , $entry , $data , $request ) { if ( $entry [ 'form_id' ] == '96' ) { if ( ! is_wp_error( $response ) && isset( $response ->data ) && is_array ( $response ->data ) ) { $response ->data[ 'entry_id' ] = $entry [ 'id' ]; } } return $response ; } |
Use request data to define entry creator by user ID
This approach could also be used with small changes to lookup based on any data that get_user_by or the WP_User has as long as the $entry[‘created_by’] update is a user ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | add_filter( 'gravityflowincomingwebhook_response_post_process' , 'jo_incoming_webhook_response_example' , 10, 4); function jo_incoming_webhook_response_example( $response , $entry , $data , $request ) { //Change to match your form ID if ( $entry [ 'form_id' ] == '1' ) { if ( ! is_wp_error( $response ) && isset( $response ->data ) && is_array ( $response ->data ) ) { $content_type = $request ->get_content_type(); if ( rgar( $content_type , 'subtype' ) === 'json' ) { $request_data = $request ->get_json_params(); } else { $request_data = $request ->get_body_params(); } //Change to match the key of your request structure if ( isset( $request_data [ 'creator' ] ) ) { $potential = get_user_by( 'id' , (int) $request_data [ 'creator' ] ); if ( $potential ) { $entry [ 'created_by' ] = $request_data [ 'creator' ]; $update = GFAPI::update_entry( $entry ); if ( is_wp_error( $update ) ) { $response ->data[ 'error' ] = $update ->get_error_message(); } else { $response ->data[ 'entry_id' ] = $entry [ 'id' ]; } } } } } return $response ; } |
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?