【发布时间】:2021-03-22 21:43:49
【问题描述】:
我们已经使用 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 像这样。我们不知道代码中有什么问题,谁能帮帮我
【问题讨论】:
标签: wordpress wordpress-theming contact-form-7