php 如何使用wordpress上的update_field在ACF上上传图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15638046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to upload an image on ACF with update_field on wordpress
提问by Lee Proctor
I have a form, which has an upload ($_FILES['watch_photo']
) field.
我有一个表单,其中有一个上传 ( $_FILES['watch_photo']
) 字段。
I have looked around and came to put this function together.
我环顾四周,想把这个功能放在一起。
It basically takes all relevant information so it is re-usable in the future, it will return an array of the $pid, and URL of the file, when it is done.
它基本上获取所有相关信息,以便将来可以重用,完成后它将返回 $pid 和文件 URL 的数组。
The problem is that ACF has not provided much information how to add images to it's fields using update_field()
http://www.advancedcustomfields.com/resources/functions/update_field/
问题是 ACF 没有提供太多信息如何使用http://www.advancedcustomfields.com/resources/functions/update_field/向其字段添加图像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'] );
}
}
}
and then I used the following code:
然后我使用了以下代码:
- to create a post and then
- upload then use
my_update_attachment
to process the image - and finally update the advanced custom field
- 创建一个帖子,然后
- 上传然后用于
my_update_attachment
处理图像 - 最后更新高级自定义字段
The code:
编码:
$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);
What am I missing? any help would be greatly appreciated.
我错过了什么?任何帮助将不胜感激。
回答by Val
I had the same problem, you almost had it right, which helped me as well.
我遇到了同样的问题,你几乎做对了,这也帮助了我。
looked at ACF and the update_field
accepts attachment ID as oppose to the the, pid
or url
查看 ACF 并update_field
接受附件 ID 作为反对,pid
或url
You can replace the return array:
您可以替换返回数组:
return array(
'pid' =>$pid,
'url' =>$file['url']
);
With the following array:
使用以下数组:
return array(
'pid' =>$pid,
'url' =>$file['url'],
'file'=>$file,
'attach_id'=>$attach_id
);
and then change
然后改变
update_field('field_512e085a9372a',$att['url'],$watch);
with the following
与以下
update_field('field_512e085a9372a',$att['attach_id'],$watch);
回答by user3719894
Check this plugin:
检查这个插件:
https://wordpress.org/plugins/acf-frontend-display/
https://wordpress.org/plugins/acf-frontend-display/
- Create ACF form with frontendUploader field (plugin extention)
- Add your ACF form into post and with post admin panel use checkbox to view on front
- 使用 frontendUploader 字段创建 ACF 表单(插件扩展)
- 将您的 ACF 表单添加到帖子中,并在帖子管理面板中使用复选框在前面查看