wordpress - 将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);
}
}
推荐阅读
- css - 使用 & 运算符将属性从父级传递给子级
- mysql - CASE 语句中的 MYSQL SUM 2 值
- javascript - 向函数添加属性并设置其原型
- python-3.x - 在axis = 3上连接数组,而第一维不同
- angular - ngModel 数据绑定不起作用
- c++ - 拖放不适用于使用 QWidget::createWindowContainer 捕获和嵌入的 Qt Widgets 应用程序
- ios - ios中自定义相机的画中画效果
- html - Angular 5在表格中呈现没有父模板的模板内容
- aurelia - 在打字稿中使用纯 javascript 隐藏引导模式
- tabs - Dynamics crm - 根据选项卡的 xrm 事件