0

I'm creating a function to hook into wpcf7_mail_sent to save the submission of a Contact Form 7 form to a json file. I'm getting the submission to the form with the following code.

$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();

if ( $submission ) {
  // The rest of the code goes in here
}

Then I have an array with output data, where I store everything before converting it to json, like this.

$output = [
  'name' => $data['name']
]

Now I also have optional fields. I put all of those in an array, to loop through it and see it they're empty or not. But somehow this is not working.

$optional_fields = ['subject', 'phone'];
foreach ( $optional_fields as $optional_field ) {
  if ( isset($data[$optional_field]) ) {
    array_push( $output[$optional_field], $data[$optional_field] );
  } 
}

No matter if I try isset(), != null or just the example above, I always get ALL optional fields in my output, also the ones where the value is null. How can I filter out the unrequited fields?

Tom
  • 387
  • 2
  • 12

1 Answers1

0

I'm unsure if i got this correctly but since your posted data is here:

$data = $submission->get_posted_data();

why you loop over this:

$optional_fields = ['subject', 'phone'];
foreach ( $optional_fields as $optional_field ) {
...

to check if data is empty or not? Shouldn't that be:

$data = $submission->get_posted_data();
$optional_fields = ['subject', 'phone'];
foreach ( $optional_fields as $optional_field ) {
    if ( isset($data[$optional_field]) && !empty($data[$optional_field]) ) {
        array_push( $output[$optional_field], $data[$optional_field] );
    }
}

Edited1:

Have you tried to var_dump the $data var and see which values you actually receive? because that if isset && !empty should be more then enough to filter out the undesidered values.

You have another fallback option which is something described here: Remove empty array elements

Sidenote for performance optimization: https://www.php.net/array-push going with array[] =.... is faster then array_push

Diego
  • 1,610
  • 1
  • 14
  • 26
  • Oh I'm sorry, I made a mistake while typing the code here. Didn't want to just copy paste the entire function with unnecessary code for the question. Updated the question. – Tom Oct 19 '20 at 13:18
  • I tried this, but in the json it's still showing a lot of 'forminput' => null items. – Tom Oct 19 '20 at 13:22
  • Yes, but every time I try to var_dump the form get's stuck in a loop when trying to send. Basically I can see the values as it's written to the json file, so that should be fine. Also when I do fill in the fields, they get stored so $data[$optional_field] should also work... You can see the generated JSON here if it helps: https://vbhworks.nl/api/daglijsten/12-12-2034.json – Tom Oct 19 '20 at 13:27
  • 1
    There must be something in that $data variable that is messing up with your ifs. Since cf7 uses AJAX you feel like you are getting stack in a loop. The quick way to test that var_dump is to place a die(); immediatly after, then with your Google Chrome inspect window open (network tab -> XHR subset) you can go and see what is returning from your request and you will understand what's wrong with the data :) – Diego Oct 19 '20 at 13:35
  • Your fallback option did the trick. Works for now, gonna dive into the other solution later tonight. Glad the client has a workable solution for now! Thank you! – Tom Oct 19 '20 at 13:38