How to Add Data to Bricks Form Custom Action

I was trying to find a way to add some custom data to the response object sent back from a Bricks custom form function.

After digging through the bricks core code I stumbled across the following hook:

// NOTE: Undocumented
$response = apply_filters( 'bricks/form/response', $response, $this );

The comments also mention this is Undocumented, hence why this may not be shown anywhere but this filter hook can be used to modify the response object before its passed back to the front end.

I added this code:

add_filter( 'bricks/form/response', 'lb_custom_form_response', 10, 2 );

function lb_custom_form_response( $response, $form ) {
    //$form_fields   = $form->get_fields();
   //$form_id       = $form_fields['formId'];
   //$results = $form->get_results();

    // Logic here  - -example to add a single extra item to the response object
    // Check if the result is a success
      if( isset( $results['success']) ) {
        // Loop over the success aray or you could use reset function
         foreach( $results['success'] as $result) {
            if ( $result['action'] === 'my_custom_action' ) {
               $response['some_data'] = $result['some_data'];
               break;
            }
         }
      }

  // Make sure you return a response!
   return $response;
}

This will add ‘some_data’ to the json object sent back from the AJAX form submission.

Hope this helps

3 Likes

Thanks for sharing :slight_smile:

1 Like

No worries - I don’t mean to be harsh but the bricks documentation is really not the best so thought it best to share to spare someone else googling or sifting through the code :slight_smile: