首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图片优化上传CF7图片数据保存到CPT的CF7图片字段中

将图片优化上传CF7图片数据保存到CPT的CF7图片字段中
EN

Stack Overflow用户
提问于 2020-12-11 21:08:21
回答 1查看 43关注 0票数 0

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

代码语言:javascript
复制
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‘规定图像’是cf7中的图像字段名称,图像另存为1191566397/ID_file_download.png,就像this.We一样不知道代码出了什么问题有人能帮我吗

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-12 19:34:02

我有一段代码可能会对你有所帮助。虽然我没有具体回答您的问题,但我相信这将为您指明正确的方向。你不应该挂接wpcf7_posted_data,而应该挂接wpcf7_before_send_mail,在那里你可以接收发布的数据。在我的例子中,我添加了一个附件,并包含了作者元数据。但我相信,有了这个,你可以弄清楚如何附加到你的ACF字段?我让$posted_data返回您上面所拥有的内容。

代码语言:javascript
复制
<?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);
        
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65251965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档