programing

wordpress에 update_field를 사용하여 ACF에 이미지를 업로드하는 방법

lovejava 2023. 2. 23. 21:56

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/

  1. frontend Uploader 필드를 사용하여 ACF 폼 작성(플러그인 확장)
  2. ACF 폼을 POST 및 Post Admin Panel use 체크박스를 사용하여 전면에 표시

언급URL : https://stackoverflow.com/questions/15638046/how-to-upload-an-image-on-acf-with-update-field-on-wordpress