问题描述
我正在尝试使用Gravity表单上传多个文件来添加WooCommerce产品库,但不应添加产品库
add_action( 'gform_after_submission_3','set_post_content',10,2 );
function set_post_content( $entry,$form ) {
if (!function_exists('wp_generate_attachment_metadata')) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
}
$uploaded_files = json_decode(rgpost("gform_uploaded_files"));
foreach($uploaded_files->input_76 as $key => $data){
if (isset($_FILES[$data])) {
$file_url = $data->input_76; //great but what is its url?
$upload_dir = wp_upload_dir(); //where do you want to put it?
$file_data = file_get_contents($file_url); //show me what you're made of
$filename = basename($file_url); //so cute but what's its name?
if (wp_mkdir_p($upload_dir['path'])) //can we put it there?
$file = $upload_dir['path'] . '/' . $filename; //yes great
else //or no,okay fine let's try somewhere else
$file = $upload_dir['basedir'] . '/' . $filename; //get the whole location
file_put_contents($file,$file_data); // tada home at last
//$wp_filetype = wp_check_filetype($filename,array('pdf' => 'application/pdf','pdf' => 'application/x-pdf') ); //is it the right type of of file?
$attachment = array(//set up the attachment
//'post_mime_type' => $wp_filetype['type'],'post_title' => sanitize_file_name($filename),'post_content' => '','post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment,$file,$entry['post_id']); //insert attachment
$attach_data = wp_generate_attachment_metadata($attach_id,$file); //asign the meta
wp_update_attachment_metadata($attach_id,$attach_data); //update the post
update_post_meta($entry['post_id'],'_product_image_gallery',$attach_id);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)