将CF7扩展插件"Images Optimize and Upload CF7"图像数据保存到CPT的ACF图像字段中



我们用cf7创建了一个表单,并用"图像优化和上传CF7";插件,现在我们想将每个文件的数据保存到cpt中,但无法将cf7图像保存到该特定cpt的acf图像字段中,这是我的代码:

function save_posted_data( $posted_data ) {

$args = array(
'post_type' => 'prescriptions',
'post_status'=>'draft',
'post_title'=>$posted_data['phonenumber'],
);
$post_id = wp_insert_post($args);
if(!is_wp_error($post_id)){
if( isset($posted_data['location']) ){
update_post_meta($post_id, 'location', $posted_data['location']);
}
if( isset($posted_data['phonenumber']) ){
update_post_meta($post_id, 'contact_number', $posted_data['phonenumber']);
}
if( isset($posted_data['prescriptiontext']) ){
update_post_meta($post_id, 'prescription_description', $posted_data['prescriptiontext']);
}
if ( !function_exists('wp_handle_upload') ) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
// Move file to media library
$movefile = wp_handle_upload( $_FILES[$posted_data['prescriptionimage']], array('test_form' => false) );
// If move was successful, insert WordPress attachment
if ( $movefile && !isset($movefile['error']) ) {
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($movefile['file']),
'post_mime_type' => $movefile['type'],
'post_title' => preg_replace( '/.[^.]+$/', "", basename($movefile['file']) ),
'post_content' => "",
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $movefile['file']);
update_field('prescription_image', $attach_id, $post_id);
}
//and so on ...
return $posted_data;
}
}
add_filter( 'wpcf7_posted_data', 'save_posted_data' );

这是我的代码,$posted_data['prescriptionimage']是cf7中的图像字段名,图像保存为1191566397/ID_file_download.png,如下所示。我们不知道代码出了什么问题有人能帮我解决吗

我有一段代码可能会对您有所帮助。如果我没有具体回答你的问题,我相信这将为你指明正确的方向。您不应该挂接wpcf7_posted_data,而应该挂接可以接收已发布数据的wpcf7_before_send_mail。在我的案例中,我添加了一个附件,并包含了作者元数据。但我相信,有了这个,你就能想出如何附加到你的ACF字段?我让$posted_data归还了你上面的东西。

<?php
add_action( 'wpcf7_before_send_mail', 'he_cf7_process_form', 10, 1);
/**
* @param object $contact_form The UserID of the Poster
*/
function he_cf7_process_form($contact_form) {
// check to make sure it only fires on your contact form
if ( $contact_form->id() == integer_of_your_contact_form ) return;
// Get the contact form submitted data
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
$uploaded_files = $submission->uploaded_files();
} else {
return;
}
// Your field name of the file goes here
$files = $_FILES['field-name'];
if (empty($uploaded_files)) {
return 0;
} else {
$image_name = basename($uploaded_files['field-name']);
$image_location = $uploaded_files['field-name'];
$image_content = file_get_contents($image_location);
$wp_upload_dir = wp_upload_dir();
$upload = wp_upload_bits($image_name, null, $image_content);
$filename = $upload['file'];
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
'post_mime_type' => $files['type'],
'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename);
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
$arg = array(
'ID' => $attach_id,
'post_author' => $userid,
);
wp_update_post($arg);

}
}

最新更新