保存CF7扩展插件"Images Optimize and Upload CF7"图像数据到CPT的ACF图像域
Saving CF7 extension plugin "Images Optimize and Upload CF7" image data into ACF image field of CPT
我们已经使用 cf7 创建了一个表单,并使用“图像优化和上传 CF7”插件创建了图像字段,现在我们想将每个提交的数据保存到 cpt,但无法将 cf7 图像保存到该特定的 acf 图像字段中cpt 这是我的代码:
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 就像 this.We 不知道是什么代码错了谁能帮帮我
我有一段代码可能会对您有所帮助。如果我没有具体回答您的问题,我相信这会为您指明正确的方向。您不应该挂钩 wpcf7_posted_data
而是挂钩 wpcf7_before_send_mail
可以接收发布数据的地方。就我而言,我添加了一个附件,并包含了作者元数据。但我相信有了这个,您可以弄清楚如何附加到您的 ACF 字段?我做了 $posted_data
return 你上面的东西。
<?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);
}
}
我们已经使用 cf7 创建了一个表单,并使用“图像优化和上传 CF7”插件创建了图像字段,现在我们想将每个提交的数据保存到 cpt,但无法将 cf7 图像保存到该特定的 acf 图像字段中cpt 这是我的代码:
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 就像 this.We 不知道是什么代码错了谁能帮帮我
我有一段代码可能会对您有所帮助。如果我没有具体回答您的问题,我相信这会为您指明正确的方向。您不应该挂钩 wpcf7_posted_data
而是挂钩 wpcf7_before_send_mail
可以接收发布数据的地方。就我而言,我添加了一个附件,并包含了作者元数据。但我相信有了这个,您可以弄清楚如何附加到您的 ACF 字段?我做了 $posted_data
return 你上面的东西。
<?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);
}
}