我可能会给你一个解决方案。
但我不得不说,我不确定我是否理解你想要什么。
下面是我一步一步地设计的:
用户将访问一个页面,需要在其中插入电子邮件才能接收pdf文件用户点击submit,我们使用AJAX将用户传递的所有数据发送到php文件中进行处理此时,联系人表单7插件已经向某人发送了消息。因此,通过管理区域,您需要选择谁将接收消息以及消息的内容。也许你可以警告用户他的pdf即将发布,这取决于你返回处理数据的php文件。在这里,我们将做两件重要的事情。首先,生成用户的pdf。第二,使用wp_mail()
功能,以便用户可以在其收件箱中接收pdf最后但并非最不重要的是,在php文件中,我们将所有数据发送到JavaScript文件,以便您可以处理这些数据,以便u构建预览因此,首先我们需要JS中的脚本。您可以将下面的代码放入您的函数中。php:
function my_script() { ?>
<script>
;(function () {
$ = jQuery.noConflict();
var form_data;
// $(parent).on(event, selector, function)
$(\'body\').on(\'submit\', \'form.wpcf7-form\', function( e ){
e.preventDefault();
// Keep all data posted through the form. Custom data can be handled later.
form_data = $(this).serialize();
$.ajax({
method : \'post\',
url : \'<?php echo get_stylesheet_directory_uri() . \'/custom.php\'; ?>\', // Put here your own php file path.
dataType : \'json\',
data : {
form_data : form_data,
},
success : function( response ) {
// Here u can do whatever u want with the response. This is what u asked for in your question.
// In your case the response will have data for u to build the preview.
// Remove the log below when running in production.
console.log( response );
},
error: function( xhr, status, error ) {
// Use this in case you are having erros and u want to see what\'s going on in your php file which is being requested by your AJAX.
// var err = eval( "(" + xhr.responseText + ")" );
// console.log( err.Message );
}
});
});
})();
</script>
<?php
}
add_action( \'wp_footer\', \'my_script\', 100 );
现在我们需要处理php文件。请参见上面的代码正在调用一个名为custom的文件。php。你可以给它取任何你想要的名字,只要确保你会为它使用正确的路径。请参见下面的“我的习惯”。php:
<?php
// We need to include wp-load.php in case we need to use WordPress functions and constants, and we will.
$absolute_path = explode( \'wp-content\', $_SERVER[\'SCRIPT_FILENAME\'] );
require_once $absolute_path[0] . \'wp-load.php\';
// Set response var.
$r = array();
// Optionally u can get rid of some unnecessary part of the serialzed string.
$_POST[\'form_data\'] = preg_replace( "/.+_wpnonce=.+&/U", \'\', $_POST[\'form_data\'] );
$fields = explode( \'&\', $_POST[\'form_data\'] );
foreach ( $fields as $field ) {
$field_pieces = explode( \'=\', $field );
// $r[\'form_data\'][\'your-name\'] = \'Your name\'.
$r[\'data\'][$field_pieces[0]] = urldecode( $field_pieces[1] );
}
// If needed, set all your custom data here.
// You can use this custom data within the pdf and also u can put it in your preview.
$r[\'data\'][\'custom_field\'] = \'CUSTOM VALUE\';
// Require PDF library. Pay attention not to get the wrong path, otherwise this file will retrieve an error.
require_once \'fpdf/fpdf.php\';
/* Init PDF */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->AliasNbPages();
$pdf->SetFont(\'Arial\',\'B\',12);
$today_date = "California, " . date("d F Y");
$pdf->Cell(0, 10, $today_date, 0, 1, \'R\');
// Make sure this path is right.
$pdf->Output(wp_upload_dir()[\'basedir\'] . \'/\' . $r[\'data\'][\'your-name\'] . \'.pdf\', \'F\');
function send_email() {
global $r;
// Here we are sending the pdf to the e-mail which was put by the user.
$to = $r[\'data\'][\'your-email\'];
// U can add more headers if u wish. Just google it.
$headers = \'From: Rod0n \' . "\\r\\n";
$subject = \'Your requested PDF has arrived\';
$msg = \'You can see your pdf attached. Thanks.\';
// Attach the pdf. e.g. array( WP_CONTENT_DIR . \'/uploads/\' . $r[\'data\'][\'your-name\'] . \'.pdf\' );
$attachment = array( \'put here the path for the pdf\' );
wp_mail( $to, $subject, $msg, $headers, $attachment );
}
send_email();
// Encode our response in order to retrieve it subsequently.
$r = json_encode( $r );
// Retrieve response var.
echo $r;
请注意这两个代码片段,尤其是注释,并设置您自己的更改。