Creating an XML file from a ContactForm7 submission
Here's how to extract raw submission data from ContactForm7 and automatically build it as an XML file on each user submission.
While ContactForm7 is a great plugin that helps you get started quickly and easily, there are a number of different reasons why you might need to manipulate form-data before actually sending the email. In this case, the clients website contained a form, where users would enter repair-requests. Think of it as kind of a simple bug-tracking form. After submitting the form, the maintenance department would get a notification mail that a new issue was reported.
As the maintenance department has its own internal ticket-system for issue-tracking, they needed a solution that would automatically take every new user-submitted issue and translate it to XML, in order for the in-house system to open a new ticket. Here's how I solved the problem:
Step 1: Get the raw data
To parse and manipulate the form data, we need to tell the contact form to give us the data the user entered, before actually sending the mail. First of all, lets take a look at how ContactForm7 stores the user-entered data.
After submitting the form, the values from each input field are stored in an Object (not in the database!). How do we know whats in there? Fortunately, the plugin provides an action hook, called wpcf7_before_send_mail
which is executed after the user submitted the form and, as the name says, right before sending the mail.
// define the callback function
function my_get_form_values($contact_form)
{
// get info about the form and current submission instance
$formTitle = $contact_form->title();
$submission = WPCF7_Submission::get_instance();
// define which form you're looking for (if you have multiple)
// and if the submission is valid
if ( $formTitle == "myFormName" && $submission ) {
// get the actual data!
$posted_data = $submission->get_posted_data();
}
}
// add the action
add_action('wpcf7_before_send_mail', 'my_get_form_values', 10, 1);
Lets take a look at the actual object. Depending on whats in your actual form, $posted_data
will look something like this:
Array
(
[_wpcf7] => 696
[_wpcf7_version] => 4.7
[_wpcf7_locale] => de_DE
[_wpcf7_unit_tag] => wpcf7-f501-t2
[SerialNumber] => 24193071
[firstname] => John
[lastname] => Doe
[email] => john@doe.com
[someCheckboxes] => Array
(
[0] => Option 1
[1] => Option 2
[2] => Option 3
)
)
Now we can easily extract all the needed values per submission. For instance, if we just cared about the users name, we could extend our simple functions to something like this:
// get the actual data!
$posted_data = $submission->get_posted_data();
$firstName = posted_data['firstname'];
$lastName = posted_data['lastname'];
From here on, you are pretty much free to do what you want with the submission data.
Step 2: Build the XML file
In my case, I had to build an xml file which would be automatically send to the clients ticket server, where it was processed and incorporated into the system. Depending on the number of form fields you have, you might approach this differently, but here's how building an XML file can look for two example values. For the sake of readability, I've created a new function which takes $posted_data
which we populated above.
function my_generate_xml($posted_data) {
// Get a serial number from the contact form,
// to distinguish report from others
$serialNr = $posted_data['SerialNumber'];
// Create xml doc spec
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$xmlDoc->formatOutput = true;
// Build Maximizer XML file
$xmlRoot = $xmlDoc->createElement('Bug report - ticket #' . $serialNr);
$xmlDoc->appendChild($xmlRoot);
// Example node for current user
$xml_User = $xmlDoc->createElement('User');
$xml_fn = $xmlDoc->createElement('firstname', $posted_data['firstname']);
$xml_User->appendChild($xml_fn);
$xml_ln = $xmlDoc->createElement('lastname', $posted_data['lastname']);
$xml_User->appendChild($xml_ln);
$xmlRoot->appendChild($xml_User);
// save it as a file for further processing
$content = chunk_split(base64_encode($xmlDoc->saveXML()));
$xmlDoc->save('ticket-'.$serialNr.'.xml');
}
And thats about it. Now, each time a user submits the form, we extract the raw data from the submission and generate an XML file. In this case, the file is created in the root directory of your WordPress installation, you are free to change the path to whatever you prefer. The unique ID (from the serial number) ensures that multiple users can submit the form at the same time and not write into one file.