admin管理员组文章数量:1410689
I´m trying to create a plugin for WordPress to collect data from a website job-application form and generate a .XML file for each appliance ... I´m learning php.
In this little code presented below, I´m just using 2 fields (Firstname
and Lastname
) but for the real-case, it would be more fields (numbers and letters)
Problem Statement:
The clients website contained a form, where users would enter job-application. After submitting the form, the HR department would get a notification mail that a new job was submitted.
As the HR department has its own internal HR-system for job-tracking, they needed a solution that would automatically take every new user-submitted application and translate it to XML, in order for the in-house system import it. Here's what I found in the internet to solve the problem:
<?php
// add the action
add_action('wpcf7_before_send_mail', 'my_get_form_values');
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();
$firstName = posted_data['firstname'];
$lastName = posted_data['lastname'];
}
}
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');
}
?>
The idea is to each time a user submits the form, we extract the raw data from the submission and generate an XML file. In this case, where the file is created? in the root directory of your WordPress installation? Also a unique ID (from the serial number) would ensures that multiple users can submit the form at the same time and not write into one file.
The plugin was installed and activated, however it´s not working as intented to be.
Any clue what I´m missing?
I´m trying to create a plugin for WordPress to collect data from a website job-application form and generate a .XML file for each appliance ... I´m learning php.
In this little code presented below, I´m just using 2 fields (Firstname
and Lastname
) but for the real-case, it would be more fields (numbers and letters)
Problem Statement:
The clients website contained a form, where users would enter job-application. After submitting the form, the HR department would get a notification mail that a new job was submitted.
As the HR department has its own internal HR-system for job-tracking, they needed a solution that would automatically take every new user-submitted application and translate it to XML, in order for the in-house system import it. Here's what I found in the internet to solve the problem:
<?php
// add the action
add_action('wpcf7_before_send_mail', 'my_get_form_values');
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();
$firstName = posted_data['firstname'];
$lastName = posted_data['lastname'];
}
}
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');
}
?>
The idea is to each time a user submits the form, we extract the raw data from the submission and generate an XML file. In this case, where the file is created? in the root directory of your WordPress installation? Also a unique ID (from the serial number) would ensures that multiple users can submit the form at the same time and not write into one file.
The plugin was installed and activated, however it´s not working as intented to be.
Any clue what I´m missing?
Share Improve this question asked Jul 9, 2018 at 23:00 WagnerWagner 33 bronze badges1 Answer
Reset to default 0The file is not created, because the function my_generate_xml
exists, but is never called.
All that code looks correct. The only problem is that you have to call the function that generates the XML file...
add_action('wpcf7_before_send_mail', 'my_get_form_values');
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();
my_generate_xml( $posted_data );
/* You don't need these two lines, so you can delete them
$firstName = posted_data['firstname'];
$lastName = posted_data['lastname'];
*/
}
}
本文标签: phpCreating a WordPress addon for ContactForm7 submission (XML file export)
版权声明:本文标题:php - Creating a WordPress addon for ContactForm7 submission (.XML file export) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744802585a2625952.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论