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?