admin管理员组文章数量:1122832
I'm aiming to submit my Contact Form 7 data in Wordpress to an API (all fields are fine, but adding the file attachment breaks my submit - error 500), which includes an attachment. I can make it work with a dummy file like so:
$attachedfile = get_template_directory() . '/test.pdf';
$file_contents = file_get_contents($attachedfile);
$base64_file = base64_encode($file_contents);
$file_name = basename($attachedfile);
$mime_type = mime_content_type($attachedfile);
$documents = [
[
'FileName' => $file_name,
'MimeType' => $mime_type,
'Base64' => $base64_file,
],
];
This sends the file to the server, and it receives it fine. But I need to do it dynamically through the form.
So I constructed this function:
function my_custom_form_submission($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$data = $submission->get_posted_data();
$email = isset($data['your-email']) ? sanitize_email($data['your-email']) : '';
$phone = 'Nincs megadva';
$lastname = isset($data['your-name']) ? sanitize_text_field($data['your-name']) : '';
$firstname = isset($data['text-532']) ? sanitize_text_field($data['text-532']) : '';
$companyname = isset($data['text-479']) ? sanitize_textarea_field($data['text-479']) : '';
$created = current_time('Y-m-d\TH:i:s');
$branch = isset($data['menu-586']) ? sanitize_textarea_field($data['menu-586']) : '';
if ($branch == 'Divat / Design') {
$flow = 'DivatDesign';
} else {
$flow = 'Startup';
}
$uploaded_files = $submission->uploaded_files();
// if (!empty($uploaded_files)) {
// $uploaded_file = $uploaded_files['file-524'];
// // Read file contents
// $file_content = file_get_contents($uploaded_file);
// if ($file_content !== false) {
// // Encode file content in base64
// $base64_file = base64_encode($file_content);
// // Prepare data array for API
// $documents = array(
// 'FileName' => $uploaded_file['name'],
// 'MimeType' => $uploaded_file['type'],
// 'Base64' => 'data:' . $uploaded_file['type'] . ';base64,' . $base64_file
// );
// }
// }
//error_log('Uploaded File Path: ' . $attachedfile);
if (wf_checkEmail($email) == false) {
$userId = saveToWf($email, $lastname, $firstname);
//Log the userId for debugging
error_log('User ID: ' . $userId);
if ($userId) {
registerToWf($email, $lastname, $firstname, $flow, $userId, $companyname, $phone, $attachedfile, $branch, $documents);
}
}
}
}
add_action('wpcf7_before_send_mail', 'my_custom_form_submission');
Issue is, if I submit my form like this, it works for all fields, however if I comment out the fields related to $uploaded_files, it breaks the submit with an 500 error. I have found no way to see (output / dump) what $uploaded_files exactly returns, but I'm guessing it's something different than my dummy file.
There is only 1 file uploaded, so I don't need a foreach. Anyone done something similar, or can point me in the right direction? I was not able to find anything on this.
- Also it's worth noting that I only have FTP access to their server, and wp_debug seems to be disabled somehow. No log is visible despite having define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', true ); define( 'WP_DEBUG_LOG', true ); in wp_config.
I'm aiming to submit my Contact Form 7 data in Wordpress to an API (all fields are fine, but adding the file attachment breaks my submit - error 500), which includes an attachment. I can make it work with a dummy file like so:
$attachedfile = get_template_directory() . '/test.pdf';
$file_contents = file_get_contents($attachedfile);
$base64_file = base64_encode($file_contents);
$file_name = basename($attachedfile);
$mime_type = mime_content_type($attachedfile);
$documents = [
[
'FileName' => $file_name,
'MimeType' => $mime_type,
'Base64' => $base64_file,
],
];
This sends the file to the server, and it receives it fine. But I need to do it dynamically through the form.
So I constructed this function:
function my_custom_form_submission($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$data = $submission->get_posted_data();
$email = isset($data['your-email']) ? sanitize_email($data['your-email']) : '';
$phone = 'Nincs megadva';
$lastname = isset($data['your-name']) ? sanitize_text_field($data['your-name']) : '';
$firstname = isset($data['text-532']) ? sanitize_text_field($data['text-532']) : '';
$companyname = isset($data['text-479']) ? sanitize_textarea_field($data['text-479']) : '';
$created = current_time('Y-m-d\TH:i:s');
$branch = isset($data['menu-586']) ? sanitize_textarea_field($data['menu-586']) : '';
if ($branch == 'Divat / Design') {
$flow = 'DivatDesign';
} else {
$flow = 'Startup';
}
$uploaded_files = $submission->uploaded_files();
// if (!empty($uploaded_files)) {
// $uploaded_file = $uploaded_files['file-524'];
// // Read file contents
// $file_content = file_get_contents($uploaded_file);
// if ($file_content !== false) {
// // Encode file content in base64
// $base64_file = base64_encode($file_content);
// // Prepare data array for API
// $documents = array(
// 'FileName' => $uploaded_file['name'],
// 'MimeType' => $uploaded_file['type'],
// 'Base64' => 'data:' . $uploaded_file['type'] . ';base64,' . $base64_file
// );
// }
// }
//error_log('Uploaded File Path: ' . $attachedfile);
if (wf_checkEmail($email) == false) {
$userId = saveToWf($email, $lastname, $firstname);
//Log the userId for debugging
error_log('User ID: ' . $userId);
if ($userId) {
registerToWf($email, $lastname, $firstname, $flow, $userId, $companyname, $phone, $attachedfile, $branch, $documents);
}
}
}
}
add_action('wpcf7_before_send_mail', 'my_custom_form_submission');
Issue is, if I submit my form like this, it works for all fields, however if I comment out the fields related to $uploaded_files, it breaks the submit with an 500 error. I have found no way to see (output / dump) what $uploaded_files exactly returns, but I'm guessing it's something different than my dummy file.
There is only 1 file uploaded, so I don't need a foreach. Anyone done something similar, or can point me in the right direction? I was not able to find anything on this.
- Also it's worth noting that I only have FTP access to their server, and wp_debug seems to be disabled somehow. No log is visible despite having define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', true ); define( 'WP_DEBUG_LOG', true ); in wp_config.
1 Answer
Reset to default 0Try this
function my_custom_form_submission( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$data = $submission->get_posted_data();
$email = isset( $data['your-email'] ) ? sanitize_email( $data['your-email'] ) : '';
$phone = 'Nincs megadva';
$lastname = isset( $data['your-name'] ) ? sanitize_text_field( $data['your-name'] ) : '';
$firstname = isset( $data['text-532'] ) ? sanitize_text_field( $data['text-532'] ) : '';
$companyname = isset( $data['text-479'] ) ? sanitize_textarea_field( $data['text-479'] ) : '';
$created = current_time( 'Y-m-d\TH:i:s' );
$branch = isset( $data['menu-586'] ) ? sanitize_textarea_field( $data['menu-586'] ) : '';
if ( $branch == 'Divat / Design' ) {
$flow = 'DivatDesign';
} else {
$flow = 'Startup';
}
$uploaded_files = $submission->uploaded_files();
$documents = [];
if ( ! empty( $uploaded_files ) ) {
$uploaded_file = $uploaded_files['file-524'];
// Read file contents
$file_content = file_get_contents( $uploaded_file );
if ( $file_content !== false ) {
// Encode file content in base64
$base64_file = base64_encode( $file_content );
// Prepare data array for API
$documents = [
[
'FileName' => basename( $uploaded_file ),
'MimeType' => mime_content_type( $uploaded_file ),
'Base64' => $base64_file,
],
];
}
}
if ( wf_checkEmail( $email ) == false ) {
$userId = saveToWf( $email, $lastname, $firstname );
// Log the userId for debugging
error_log( 'User ID: ' . $userId );
if ( $userId ) {
registerToWf( $email, $lastname, $firstname, $flow, $userId, $companyname, $phone, $attachedfile, $branch, $documents );
}
}
}
}
add_action( 'wpcf7_before_send_mail', 'my_custom_form_submission' );
Make sure the file-524
matches the name of the file upload field in your Contact Form 7 setup. Also, ensure the file permissions allow reading the uploaded file on the server.
To debug the issue, you can temporarily add error logging to check the contents of $uploaded_files
:
error_log( print_r( $uploaded_files, true ) );
This will help you see what $uploaded_files
actually contains and adjust your code accordingly.
本文标签: rest apiWordpressContact Form 7Get uploaded file issue
版权声明:本文标题:rest api - Wordpress - Contact Form 7 - Get uploaded file issue 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306096a1932830.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论