如何使用UPDATE_FIELD在前端上传多幅图像到ACF图库

时间:2014-05-28 作者:SPi

我正在尝试保存在前端表单上提交的图像,使其可以通过ACF后端访问。

在里面how to upload an image on ACF with update_field on wordpress, 有人提供了一个单一图像上传的解决方案(以及一个ACF图像字段)。我认为将其扩展到多图像上传(和ACF gallery字段)应该很容易,但我遇到了一个无法追踪的错误:Cannot redeclare wp_crop_image() in /../travel/wp-admin/includes/image.php on line 25

in functions.php

function RemapFilesArray($name, $type, $tmp_name, $error, $size)
{
    return array(
        \'name\' => $name,
        \'type\' => $type,
        \'tmp_name\' => $tmp_name,
        \'error\' => $error,
        \'size\' => $size,
    );
}

function my_update_attachment($f,$pid,$t=\'\',$c=\'\') {
  wp_update_attachment_metadata( $pid, $f );
  if( !empty( $f[\'name\'] )) { //New upload
    require_once( ABSPATH . \'wp-admin/includes/file.php\' );
    include( ABSPATH . \'wp-admin/includes/image.php\' );

    $override[\'test_form\'] = false;
    $file = wp_handle_upload( $f, $override );

    if ( isset( $file[\'error\'] )) {
      return new WP_Error( \'upload_error\', $file[\'error\'] );
    }

    $file_type = wp_check_filetype($f[\'name\'], array(
      \'jpg|jpeg\' => \'image/jpeg\',
      \'gif\' => \'image/gif\',
      \'png\' => \'image/png\',
    ));
    if ($file_type[\'type\']) {
      $name_parts = pathinfo( $file[\'file\'] );
      $name = $f[\'name\'];
      $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( $attach_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\'],
  \'file\'=>$file,
  \'attach_id\'=>$attach_id
   );
    }
  }
}

on my form.php:

<input type="file" name="upload_attachments[]">
<input type="file" name="upload_attachments[]">

in process-form.php

if ($_FILES[\'upload_attachment\']){

 $files = array_map(\'RemapFilesArray\',
 $_FILES[\'upload_attachment\'][\'name\'],
 $_FILES[\'upload_attachment\'][\'type\'],
 $_FILES[\'upload_attachment\'][\'tmp_name\'],
 $_FILES[\'upload_attachment\'][\'error\'],
 $_FILES[\'upload_attachment\'][\'size\']
);

$gallery=array();

foreach ($files as $file){ //loop through each file
    $att = my_update_attachment($file,$site_id);
    array_push($gallery,$att[\'attach_id\']);
}
update_field(\'field_1231242\',$gallery,$site_id);

}
我感谢你的帮助和想法!

1 个回复
SO网友:SPi

Argh。。易于修复:

而不是

include( ABSPATH . \'wp-admin/includes/image.php\' );

使用

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

希望这对别人有帮助!

结束
如何使用UPDATE_FIELD在前端上传多幅图像到ACF图库 - 小码农CODE - 行之有效找到问题解决它

如何使用UPDATE_FIELD在前端上传多幅图像到ACF图库

时间:2014-05-28 作者:SPi

我正在尝试保存在前端表单上提交的图像,使其可以通过ACF后端访问。

在里面how to upload an image on ACF with update_field on wordpress, 有人提供了一个单一图像上传的解决方案(以及一个ACF图像字段)。我认为将其扩展到多图像上传(和ACF gallery字段)应该很容易,但我遇到了一个无法追踪的错误:Cannot redeclare wp_crop_image() in /../travel/wp-admin/includes/image.php on line 25

in functions.php

function RemapFilesArray($name, $type, $tmp_name, $error, $size)
{
    return array(
        \'name\' => $name,
        \'type\' => $type,
        \'tmp_name\' => $tmp_name,
        \'error\' => $error,
        \'size\' => $size,
    );
}

function my_update_attachment($f,$pid,$t=\'\',$c=\'\') {
  wp_update_attachment_metadata( $pid, $f );
  if( !empty( $f[\'name\'] )) { //New upload
    require_once( ABSPATH . \'wp-admin/includes/file.php\' );
    include( ABSPATH . \'wp-admin/includes/image.php\' );

    $override[\'test_form\'] = false;
    $file = wp_handle_upload( $f, $override );

    if ( isset( $file[\'error\'] )) {
      return new WP_Error( \'upload_error\', $file[\'error\'] );
    }

    $file_type = wp_check_filetype($f[\'name\'], array(
      \'jpg|jpeg\' => \'image/jpeg\',
      \'gif\' => \'image/gif\',
      \'png\' => \'image/png\',
    ));
    if ($file_type[\'type\']) {
      $name_parts = pathinfo( $file[\'file\'] );
      $name = $f[\'name\'];
      $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( $attach_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\'],
  \'file\'=>$file,
  \'attach_id\'=>$attach_id
   );
    }
  }
}

on my form.php:

<input type="file" name="upload_attachments[]">
<input type="file" name="upload_attachments[]">

in process-form.php

if ($_FILES[\'upload_attachment\']){

 $files = array_map(\'RemapFilesArray\',
 $_FILES[\'upload_attachment\'][\'name\'],
 $_FILES[\'upload_attachment\'][\'type\'],
 $_FILES[\'upload_attachment\'][\'tmp_name\'],
 $_FILES[\'upload_attachment\'][\'error\'],
 $_FILES[\'upload_attachment\'][\'size\']
);

$gallery=array();

foreach ($files as $file){ //loop through each file
    $att = my_update_attachment($file,$site_id);
    array_push($gallery,$att[\'attach_id\']);
}
update_field(\'field_1231242\',$gallery,$site_id);

}
我感谢你的帮助和想法!

1 个回复
SO网友:SPi

Argh。。易于修复:

而不是

include( ABSPATH . \'wp-admin/includes/image.php\' );

使用

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

希望这对别人有帮助!

相关推荐

Organizing media uploads

我的一个客户想把他的WordPress网站转移到我的服务器上。问题是他的网站没有在他的上传文件夹中使用子文件夹,而他上传文件夹的根目录中有超过1000000个文件。有没有一种方法可以将他的所有帖子上传到文件夹中,而不会丢失帖子中的附加链接和特色图片?