问题
I am trying to export the values that users input into Contact form 7 in WordPress, to PDF via fpdf. This is what I've set up, I can generate a PDF but without the dynamically generated value from the form.
functions.php
add_action( 'wpcf7_before_send_mail', 'save_application_form');
function save_application_form($cf7) {
/* GET EXTERNAL CLASSES */
require(TEMPLATEPATH.'/fpdf/fpdf.php');
$values = $cf7->posted_data;
echo $values['first-name'];
/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5,'first-name');
$pdf->SetFont('Arial','B',16);
$pdf->Output(TEMPLATEPATH.'/fpdf/pdf.pdf', 'F');
/* add the pdf as attach to the email*/
$cf7->uploaded_files = array ( 'attachedfile' => TEMPLATEPATH.'/fpdf/pdf.pdf' );
How can I pull the content from Contact form 7? Now if I press send I only get a PDF with "first name" written on it. I've tried multiple combinations, nothing works.
Thank you for your help.
EDIT: I have figured out how to print, but it seems like the problem is, that I am not pulling the inserted content from Contact Form 7.
$first_name = $cf7->posted_data["first-name"];
$var = "test";
/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5, "My car is " . $var . "bl");
$pdf->SetFont('Arial','B',16);
So $first_name doesn't work because it is empty, any ideas how i can correct this? Because if i try with $var it works.
回答1:
the solution above by Kory works perfectly. However, it doesn't work with radio buttons. All of the radio buttons are only displaying as "Array" on the final PDF. How do I display the radio button choices properly? The code I'm using is below. Thanks!
add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
function wpcf7_update_email_body($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
/* DEFINE CONSTANT AND GET FPDF CLASSES */
define ('FPDF_PATH',get_stylesheet_directory().'/fpdf17/'); // MAKE SURE THIS POINTS TO THE DIRECTORY IN YOUR THEME FOLDER THAT HAS FPDF.PHP
require(FPDF_PATH.'fpdf.php');
$posted_data = $submission->get_posted_data();
// SAVE FORM FIELD DATA AS VARIABLES
$name = $posted_data["your-name"];
$name2 = $posted_data["your-name2"];
$email = $posted_data["your-email"];
$enhetsnr = $posted_data["number-363"];
$radio220 = $posted_data["radio-220"];
$radio221 = $posted_data["radio-221"];
$radio222 = $posted_data["radio-222"];
$radio223 = $posted_data["radio-223"];
$radio224 = $posted_data["radio-224"];
$radio225 = $posted_data["radio-225"];
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Times','',16);
$pdf->Write(5, $name . "\n\n" . $name2 . "\n\n" . $email . "\n\n" . $enhetsnr . "\n\n" . $radio220 . "\n\n" . $radio221 . "\n\n" . $radio222 . "\n\n" . $radio223 . "\n\n" . $radio224 . "\n\n" . $radio225);
$pdf->Output(FPDF_PATH.'tillval.pdf', 'F'); // OUTPUT THE NEW PDF INTO THE SAME DIRECTORY DEFINED ABOVE
}
}
add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' );
function mycustom_wpcf7_mail_components($components){
if (empty($components['attachments'])) {
$components['attachments'] = array(FPDF_PATH .'tillval.pdf'); // ATTACH THE NEW PDF THAT WAS SAVED ABOVE
}
return $components;
}
回答2:
You will need get the $first_name from the POST data. This should work:
$first_name = $_POST["first-name"];
/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5, "My car is " . $first_name . "bl");
$pdf->SetFont('Arial','B',16);
回答3:
Since version 3.9 of Contact From 7, instead of using $cf7->posted_data, you can retrieve the posted data with:
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
Now you have an array with the posted data which you can use to generate the PDF file:
/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5, "My first name is: " . $posted_data['first-name'] );
$pdf->SetFont('Arial','B',16);
回答4:
I needed to accomplish the same thing and finally got the Contact Form 7 results to be converted to a PDF. I ended up using a combination of suggestions mentioned in a few forums, this one included.
You should be able to adapt this to your own purposes.
add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
function wpcf7_update_email_body($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
/* DEFINE CONSTANT AND GET FPDF CLASSES */
define ('FPDF_PATH',get_template_directory().'/fpdf/'); // MAKE SURE THIS POINTS TO THE DIRECTORY IN YOUR THEME FOLDER THAT HAS FPDF.PHP
require(FPDF_PATH.'fpdf.php');
$posted_data = $submission->get_posted_data();
// SAVE FORM FIELD DATA AS VARIABLES
$name = $posted_data["your-name"];
$email = $posted_data["your-email"];
$subject = $posted_data["your-subject"];
$message = $posted_data["your-message"];
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Write(5,$name . "\n\n" . $email . "\n\n" . $subject . "\n\n" . $message);
$pdf->Output(FPDF_PATH.'test.pdf', 'F'); // OUTPUT THE NEW PDF INTO THE SAME DIRECTORY DEFINED ABOVE
}
}
add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' );
function mycustom_wpcf7_mail_components($components){
if (empty($components['attachments'])) {
$components['attachments'] = array(FPDF_PATH .'test.pdf'); // ATTACH THE NEW PDF THAT WAS SAVED ABOVE
}
return $components;
}
回答5:
Don't forget to use a Child-Theme so your extra code in functions.php doesn't disappear when keeping the theme up to date. Having said that I had no issues above (credit to Kory).
In order to keep the /fpdf/ folder in the child-theme there is a new WP command: get_theme_file_path()
, which the code from Kory uses.
https://wordpress.stackexchange.com/questions/192773/override-get-template-directory-in-child-theme
来源:https://stackoverflow.com/questions/27094291/exporting-form-results-from-contact-form-7-to-pdf-fpdf