Nach Upload-Taste erhalten Sie eine Datei BLOB erhalten, indem Sie $ _FILES
$file = $_FILES['filename'];
$filename = $file["tmp_name"]
$upload_file = wp_upload_bits($filename, null, $file);
if (!$upload_file['error']) {
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $parent_post_id,
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
//If you want to insert into a post or custom posttype, then make $parent_post_id = 0
//Otherwise $parent_post_id = id of post/posttype
$attachment_id = wp_insert_attachment($attachment, $upload_file['file'], $parent_post_id);
if (!is_wp_error($attachment_id)) {
$attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
wp_update_attachment_metadata($attachment_id, $attachment_data);
//1- get the url
$url = wp_get_attachment_url($id);
//2-get current logged in user ID
$user_id = get_current_user_id();
add_user_meta($user_id, 'uploaded_attachments', $url);
}else{
//Failed with message = $attachment_id->get_error_message()
}
}else{
//Error : file upload failed
}