ACF。如何使用文件更新转发器字段?

时间:2019-02-27 09:37:54

标签: wordpress advanced-custom-fields

我正在尝试通过前端文件更新我的转发器字段。如果只有一个字段文件-它可以工作。但是,当我尝试用文件填充 repeater 时,它不起作用。

// JS. Ajaxly sending files
var ajaxurl = '<?php echo site_url() ?>/wp-admin/admin-ajax.php';

$('.js-new-msg-send').click(function (e) {

    e.preventDefault();
    var form = document.forms.namedItem("new_theme");

    var formData = new FormData(form);
    formData.append('user_id', '<?php echo $userID; ?>');
    formData.append('action', 'msg_to_acf');

    var xhr = new XMLHttpRequest();
    xhr.open('POST', ajaxurl, true);
    xhr.send(formData);

})

我发现了处理文件上传到acf字段的功能,并对其进行了修改以处理$ _FILES变量中的多个文件:

// FOR MULTIPLE FILES 
if ( !empty($_FILES[$f]['name']) && is_array($_FILES[$f]['name']) ) {

    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    $file_list = array();
    foreach ($_FILES[$f]['name'] as $key => $value) {
        $temp_file = array(
            'name'     => $_FILES[$f]['name'][$key],
            'type'     => $_FILES[$f]['type'][$key],
            'tmp_name' => $_FILES[$f]['tmp_name'][$key],
            'error'    => $_FILES[$f]['error'][$key],
            'size'     => $_FILES[$f]['size'][$key]
        );

        wp_update_attachment_metadata( $pid, $temp_file );
        $file = wp_handle_upload( $temp_file , array('test_form' => FALSE, 'action' => 'editpost') );
        if ( isset( $file['error'] )) {
        return new WP_Error( 'upload_error', $file['error'] );
        }
        $file_type = wp_check_filetype($temp_file['name'], array(
            'jpg|jpeg' => 'image/jpeg',
            'gif' =>      'image/gif',
            'png' =>      'image/png',
        ));


        if ($file_type['type']) {

            $name_parts = pathinfo( $file['file'] );
            $name = $file['filename'];
            $type = $file['type'];
            $title = $t ? $t : $name;
            $content = $c;

            $attachment = array(
                'post_title' => $title,
                'post_type' => 'attachment',
                'post_content' => $content,
                'post_parent' => $pid,
                'post_mime_type' => $type,
                'guid' => $file['url'],
            );

            foreach( get_intermediate_image_sizes() as $s ) {
                $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
                $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
                $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
                $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
            }

            $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );

            foreach( $sizes as $size => $size_data ) {
                $resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
                if ( $resized )
                $metadata['sizes'][$size] = $resized;
            }

            $attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);

            if ( !is_wp_error( $id )) {
                $attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
                wp_update_attachment_metadata( $attach_id, $attach_meta );
            }

            $final_temp_arr = array(
                'pid' =>$pid,
                'url' =>$file['url'],
                'file'=>$file,
                'attach_id'=>$attach_id
            );
            array_push($file_list, $final_temp_arr);    
        }
    }
    return $file_list;
}

最后,我使用此函数来获取要插入中继器的带有 attach-id 变量的信息数组。

        $att = my_update_attachment( 'msg-file-list' , $post_id );
        $files_final_list = array();
        foreach ($att as $key => $val) {
            array_push($files_final_list, $val['attach_id']);
        }
        $value_arr = array(
            array(
                "msg-author"     => $user_name,
                "msg-date"       => $date,
                "msg-read-check" => true,
                "msg-cont"       => $msg_cont,
                'msg-file-list'  => $files_final_list
            )
        );
        update_field( 'test_file_list' , $files_final_list , $post_id );

我猜那是最后一部分的错误,我试图将文件上传到转发器。有人可以告诉我我做错了什么吗?感谢您的帮助。

0 个答案:

没有答案