How to upload images via REST API
-
I’m uploading images via the rest-api. So far I’m able to upload the file to the directory, but in the admin panel I see an empty image, because the image source is set to “default.png/jpeg”. Why isn’t using the file name?
$json = $request->get_file_params(); $json_encode = json_encode($json); $files = json_decode($json_encode, true); $upload_dir = wp_upload_dir(); $file_name = $files['image']['name']; $file_to_upload = $upload_dir['path']."/".$file_name; $file_array = array('name' => $file_name, 'type' => $files['image']['type'], 'size' => $files['image']['size'], 'tmp_name' => $files['image']['tmp_name'] ); $filetype = wp_check_filetype( basename( $file_to_upload ), null ); $attachment = array( 'guid' => $upload_dir['url'] . '/' . basename( $file_to_upload ), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_to_upload ) ), 'post_content' => '', 'post_status' => 'inherit' ); require_once( ABSPATH . 'wp-admin/includes/image.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); $uploadedfile = $request->get_file_params(); $upload_overrides = array( 'test_form' => false ); $movefile = wp_handle_upload( $file_array, $upload_overrides ); $attach_id = wp_insert_attachment($attachment, $movefile['file'], 0);
[Moderated to improve post title]
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to upload images via REST API’ is closed to new replies.