I installed Contact Form 7 and I would like to access the form data after or before the email is sent. Ideally I don't need an email to be sent, I just need the form data in order to populate custom fields
What I found so far is this:
add_action('wpcf7_mail_sent', function ($cf7) {
// Run code after the email has been sent
echo $cf7;
die();
});
After adding the above action to functions.php
, the form action never completes. I want to view the data, find the postID and create the custom fields.
Any help is appreciated.
The die() prevents the code from running, remove it and it should work as normal.
Now about the from data, you will need to get the form instance and then you will have access to all the form data.
Here's an example
add_action('wpcf7_mail_sent', function ($cf7) {
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$cf7_data = $submission->get_posted_data();
}
}
Now because all of this happens in the backend you will not be able to echo any of it to check what you get.
I use phps error_log function for this. Heres a snippet:
error_log(print_r($your_value_here, true), 3, __DIR__ . '/temp-log.txt');
error_log("\r\n\r\n", 3, __DIR__ . '/temp-log.txt');
This will create a file, named temp-log.txt in the root of your theme with the data that you passed to error_log. Change $your_value_here to what ever you need.
Hope this helps =]