问题陈述: 如何从ContactForm 7提交中获取数据?
这是我在互联网上找到的解决问题的方法:
<?php
$formTitle = $contact_form->title();
$submission = WPCF7_Submission::get_instance();
if ( $formTitle == "myFormName" && $submission ) {
// get the actual data!
$posted_data = $submission->get_posted_data();
$firstName = posted_data['firstname'];
$lastName = posted_data['lastname'];
}
}
?>
发布于 2018-08-20 21:34:02
这是完整的工作代码。
// add contact form 7 hook
add_action('wpcf7_before_send_mail', 'my_get_form_values');
function my_get_form_values($contact_form)
{
if ($contact_form->id==11) //your contact form 7 id
{
// get info about the form and current submission instance
$submission = WPCF7_Submission::get_instance();
if ( $submission )
{
$posted_data = $submission->get_posted_data();
my_generate_xml($posted_data);
}
}
}
function my_generate_xml($posted_data)
{
// get a serial number from the contact form to distinguish report from others
$serial_no = $posted_data['SerialNumber'];
// create xml doc spec
$xml_doc = new DOMDocument('1.0', 'UTF-8');
$xml_doc->formatOutput = true;
// build maximizer xml file, do not use special characters
$xml_root = $xml_doc->createElement('bug_report_' . $serial_no);
$xml_doc->appendChild($xml_root);
// create node for current user
$xml_user = $xml_doc->createElement('User');
$xml_fn = $xml_doc->createElement('firstname', $posted_data['firstname']);
$xml_user->appendChild($xml_fn);
$xml_ln = $xml_doc->createElement('lastname', $posted_data['lastname']);
$xml_user->appendChild($xml_ln);
$xml_root->appendChild($xml_user);
// save it as a file for further processing
$content = chunk_split(base64_encode($xml_doc->saveXML()));
$uploads = wp_upload_dir();
$xml_doc->save($uploads['basedir'].'/prorh/'.(int)microtime(true).'-ticket-'.$serial_no.'.xml');
}
https://stackoverflow.com/questions/-100002306
复制相似问题