我正在尝试从前端提交woocommerce产品 并且有一个包含 HTML5文件控件的表单,用于将文件上传到WordPress并将其设置为产品(发布)功能图片
我有一个功能,可以从文件中以表单的形式上传图像并上传到媒体库中,然后将该图像作为发布图像
我正在尝试使用 URL而不是文件
这是功能
'image' => isset( $_FILES['wcj_add_new_product_image'] ) ? $_FILES['wcj_add_new_product_image'] : '',
。
if ( '' != $args['image'] && '' != $args['image']['tmp_name'] ) {
$upload_dir = wp_upload_dir();
$filename = $args['image']['name'];
$file = ( wp_mkdir_p( $upload_dir['path'] ) ) ? $upload_dir['path'] : $upload_dir['basedir'];
$file .= '/' . $filename;
move_uploaded_file( $args['image']['tmp_name'], $file );
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $product_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $product_id, $attach_id );
}