wordpress에 update_field를 사용하여 ACF에 이미지를 업로드하는 방법
업로드가 있는 폼이 있습니다.$_FILES['watch_photo']
) 필드.
둘러보고 이 기능을 갖추게 되었습니다.
기본적으로는 모든 관련 정보를 가지고 있기 때문에 나중에 재사용할 수 있습니다.또한 작업이 완료되면 $pid와 파일의 URL 배열을 반환합니다.
문제는 ACF가 이미지를 필드에 추가하는 방법에 대해 많은 정보를 제공하지 않았다는 것입니다.update_field()
http://www.advancedcustomfields.com/resources/functions/update_field/
function my_update_attachment($f,$pid,$t='',$c='') {
wp_update_attachment_metadata( $pid, $f );
if( !empty( $_FILES[$f]['name'] )) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$file = wp_handle_upload( $_FILES[$f], $override );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$file_type = wp_check_filetype($_FILES[$f]['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 );
}
return array(
'pid' =>$pid,
'url' =>$file['url']
);
// update_post_meta( $pid, 'a_image', $file['url'] );
}
}
}
그리고 다음 코드를 사용했습니다.
- 포스트를 만들고 나서
- 업로드 후 사용
my_update_attachment
이미지를 처리하다 - 마지막으로 고급 사용자 지정 필드를 업데이트합니다.
코드:
$args= array( 'post_type' => 'watches', 'post_status' => 'publish' );
$watch = wp_insert_post($args);
$att = my_update_attachment('watch_image',$watch);
update_field('field_512e085a9372a',$att['url'],$watch);
제가 무엇을 빠뜨리고 있나요?어떤 도움이라도 주시면 대단히 감사하겠습니다.
나도 같은 문제가 있었어, 네가 거의 맞힐 뻔했어, 그게 나한테도 도움이 됐어.
ACF와update_field
가 아닌 첨부 파일 ID 를 받아들입니다.pid
또는url
반환 어레이를 교환할 수 있습니다.
return array(
'pid' =>$pid,
'url' =>$file['url']
);
다음 어레이의 경우:
return array(
'pid' =>$pid,
'url' =>$file['url'],
'file'=>$file,
'attach_id'=>$attach_id
);
그 후 변경
update_field('field_512e085a9372a',$att['url'],$watch);
다음과 같이
update_field('field_512e085a9372a',$att['attach_id'],$watch);
이 행이 되면
if ( !is_wp_error( $id )) {
있다
if ( !is_wp_error( $attach_id )) {
이 플러그인 확인:
https://wordpress.org/plugins/acf-frontend-display/
- frontend Uploader 필드를 사용하여 ACF 폼 작성(플러그인 확장)
- ACF 폼을 POST 및 Post Admin Panel use 체크박스를 사용하여 전면에 표시
언급URL : https://stackoverflow.com/questions/15638046/how-to-upload-an-image-on-acf-with-update-field-on-wordpress
'programing' 카테고리의 다른 글
Angular Http - 약속 또는 구독 (0) | 2023.02.23 |
---|---|
$window 또는 $location을 사용한 각도 리다이렉트JS (0) | 2023.02.23 |
$.ajax 유틸리티의 JQuery 오류 옵션 (0) | 2023.02.23 |
React 클래스 구성 요소에서 작업 전후의 로드 상태 설정 (0) | 2023.02.23 |
Bean Validation 공급자를 찾을 수 없기 때문에 구성을 만들 수 없습니다.클래스 경로에 휴지 상태 검증 프로그램(RI) 등의 공급자 추가 (0) | 2023.02.23 |