ฉันใช้แบบฟอร์มติดต่อ 7 (5.4) และ CF7 Smart Grid Design Extension (4.10.0) ในแบบฟอร์มส่งบนเว็บไซต์ WordPress ของฉันเพื่อสร้างประเภทโพสต์แบบกำหนดเองแบบร่าง แรงบันดาลใจดั้งเดิมของสิ่งนี้คือโพสต์ SO ที่ยอดเยี่ยมนี้
แบบฟอร์มการส่งทำงานได้ดีและเชื่อถือได้มานานกว่าหนึ่งปี แต่ด้วยเวอร์ชัน CF7 5.4 ใหม่ มีบางอย่างเปลี่ยนแปลงไปภายในและการอัปโหลดรูปภาพที่เสียหาย ส่วนช่องอื่นๆ ทั้งหมดยังคงไม่เปลี่ยนแปลง
ฟิลด์ CF7 ที่ฉันกำลังอัปโหลดปรากฏเป็น
[file* field_image limit:10000000 filetypes:jpg|gif|png|jpeg class:field-image]
รูปภาพจะถูกส่งไป ดังนั้นแบบฟอร์มจึงอัปโหลดไฟล์ได้อย่างถูกต้อง แต่มีบางอย่างเปลี่ยนไปซึ่งทำให้ความพยายามของฉันในการจับภาพก่อนที่จะถูกลบหลังจากส่งอีเมล
นี่คือ PHP ที่เกี่ยวข้องใน Functions.php ที่จัดการภาพที่อัปโหลดภายในฟังก์ชันที่สร้าง WP CPT
// Handle CF7 form submission and populate new CPT 'Thing'
// Ref: https://wordpress.stackexchange.com/questions/328429/how-to-save-contact-form-7-data-in-custom-post-types-cpt
//
add_action('wpcf7_before_send_mail', 'save_my_form_data_to_my_cpt', 10, 3);
function save_my_form_data_to_my_cpt($contact_form){
//Get the form ID
$contact_form = WPCF7_ContactForm::get_current();
$contact_form_id = $contact_form -> id;
$submission = WPCF7_Submission::get_instance();
if (!$submission){
return $contact_form;
}
$posted_data = $submission->get_posted_data();
//The Sent Fields are now in an array
//you can now access them with $posted_data['my-email']
//
// create new post array
$new_post = array();
//
// META META FIELDS
// post_type (i.e., your CPT)
$new_post['post_type'] = 'thing';
// post_status (draft, publish, pending)
$new_post['post_status'] = 'draft';
//
// POST FIELDS
// post_title
if(isset($posted_data['field_title']) &&
!empty($posted_data['field_title'])){
$new_post['post_title'] = $posted_data['field_title'];
} else {
$new_post['post_title'] = '[Insert Title Here]';
}
// POST CPT
//When everything is prepared, insert post into WP Database
if($post_id = wp_insert_post($new_post)){
// it worked so let's continue...
//
// IMAGE
// Retrieving and inserting uploaded image as featured image
// CF7 uploads the image and puts it in a temporary directory,
// deleting it after the mail is sent
// Before it deletes it, we will move into our media library,
// and attach it to our post
//
// get file upload info from form
$uploadedFiles = $submission->uploaded_files();
// if we have an uploaded image...
if( isset($posted_data['field_image']) ){
// move image from temp folder to upload folder
$imageUpload = wp_upload_bits($posted_data['field_image'], null,
file_get_contents($uploadedFiles['field_image']));
//
require_once(ABSPATH . 'wp-admin/includes/admin.php');
// construct array to register this image
$filename = $imageUpload['file'];
$attachment = array(
'post_mime_type' => $imageUpload['type'],
'post_parent' => $post_id,
'post_title' => $posted_data['field_title'] . ' - ' .
$posted_data['field_contributor'],
'post_content' => $posted_data['field_info'],
'post_status' => 'inherit'
);
// attach image to this post
$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
// if we succeeded...
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
// add image id (attchment id) to ad_image field
update_field( 'ad_image', $attachment_id, $post_id );
}
}
}
}
สิ่งนี้ถูกเรียกโดย:
add_action('wpcf7_before_send_mail', 'save_my_form_data_to_my_cpt', 10, 3);
รูปภาพที่ส่งในอีเมล CF7 ได้สำเร็จไม่ได้รับการอัปโหลดไปยัง CPT ใหม่ที่กำลังสร้าง
แม้ว่าปัญหาอาจอยู่ที่โค้ดโพสต์ส่งที่จัดการการส่ง แต่ไม่มีสิ่งใดเปลี่ยนแปลงในหนึ่งปี มีเพียง CF7 เท่านั้นที่เปลี่ยนไป
มีคำแนะนำอะไรบ้าง?
ภาคผนวก A: ฟังก์ชันที่สมบูรณ์
นี่คือฟังก์ชัน save_my_form_data_to_my_cpt
ทั้งหมด:
~ลบแล้ว~
ภาคผนวก B: เอาต์พุตการแก้ไขจุดบกพร่อง
ในช่วงต้นของฟังก์ชันที่เราเรียกว่ารับข้อมูลที่ส่งผ่านจากแบบฟอร์ม CF7:
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();
$uploadedFiles = $submission->uploaded_files();
เมื่อเราถ่ายโอนตัวแปรเหล่านี้ไปยังคอนโซล เราจะได้รับความประหลาดใจ สำหรับ $posted_data เราได้รับ:
array (
'field_title' => 'TEST',
'field_expires' => '1',
'field_tags' =>
array (
0 => 'Children',
),
'field_addl_tags' => '',
'field_info' => '',
'field_call_to_action' => '',
'field_phone_contact' => '',
'field_email_contact' => '',
'field_website' => '',
'field_location' => '',
'field_contributor' => 'Wes Modes',
'field_notes' => '',
'field_image' =>
array (
0 => '/srv/data/web/vhosts/unavoidabledisaster.com/htdocs/wp-content/uploads/wpcf7_uploads/0318808318/left-boob.jpg',
),
'mc4wp_checkbox' => 'No',
)
สำหรับ $uploadedFiles เราได้รับ:
array (
'field_image' =>
array (
),
)
ดังนั้นแม้ว่าอาร์เรย์ไฟล์จะพร้อมใช้งานภายในข้อมูลที่โพสต์ แต่ก็ไม่สามารถใช้งานได้จากฟังก์ชันที่ออกแบบมาเพื่อให้ได้มา
สิ่งนี้หมายความว่า?
วิธีแก้ปัญหา
จริงๆ แล้วมีปัญหาอยู่สองประการ:
- ตามที่ @howard_e แนะนำผู้พัฒนา CF7 เปลี่ยนฟิลด์อัพโหลดเป็นอาร์เรย์ (อาจอนุญาตให้อัปโหลดหลายไฟล์ได้) สิ่งนี้ทำให้โค้ดที่มีอยู่เสียหาย
- เมธอด
WPCF7_Submission::get_instance()->uploaded_files()
หยุดส่งคืนไฟล์ที่อัพโหลด แม้ว่าจะไม่ส่งคืนภายในวัตถุฟิลด์อัพโหลดของWPCF7_Submission::get_instance()
นี่คือส่วนที่เกี่ยวข้องที่ได้รับการแก้ไขของฟังก์ชัน:
add_action('wpcf7_before_send_mail', 'save_my_form_data_to_my_cpt');
function save_my_form_data_to_my_cpt($contact_form){
//Get the form ID
$contact_form_id = $contact_form->id;
$submission = WPCF7_Submission::get_instance();
if (!$submission){
return; // exit the hook
}
// get posted data
$posted_data = $submission->get_posted_data();
// get file upload info from form
$uploadedFiles = $posted_data['field_image'];
// create new post array
$new_post = array();
//
// META META FIELDS
// post_type (i.e., your CPT)
$new_post['post_type'] = 'thing';
// post_status (draft, publish, pending)
$new_post['post_status'] = 'draft';
//
// POST FIELDS
// post_title
if(isset($posted_data['field_title']) &&
!empty($posted_data['field_title'])){
$new_post['post_title'] = $posted_data['field_title'];
} else {
$new_post['post_title'] = '[Insert Title Here]';
}
// POST CPT
//When everything is prepared, insert the post into WP Database
if($post_id = wp_insert_post($new_post)){
// it worked so let's continue...
//
// if we have an uploaded image...
if (!empty($uploadedFiles)) {
// move image from temp folder to upload folder
$file = file_get_contents($uploadedFiles[0]);
$image_name = basename($uploadedFiles[0]);
$imageUpload = wp_upload_bits(basename($uploadedFiles[0]), null, $file);
require_once(ABSPATH . 'wp-admin/includes/admin.php');
// construct array to register this image
$filename = $imageUpload['file'];
$attachment = array(
'post_mime_type' => $imageUpload['type'],
'post_parent' => $post_id,
'post_title' => $posted_data['field_title'] . ' - ' .
$posted_data['field_contributor'],
'post_content' => $posted_data['field_info'],
'post_status' => 'inherit'
);
// attach image to this post
$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
// if we succeeded...
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
// add image id (attchment id) to ad_image field
update_field( 'ad_image', $attachment_id, $post_id );
}
}
}
}
save_my_form_data_to_my_cpt
ทั้งหมดได้หรือไม่ - person Howard E   schedule 03.04.2021$post_id
ควรเป็นโพสต์หลักของแบบฟอร์มหรือไม่ ดูเหมือนจะไม่ได้กำหนดไว้ - person Howard E   schedule 04.04.2021