$upload_dir['url'] . '/' . basename( $new_file_path ), 'post_mime_type' => $filetype['type'], 'post_title' => sanitize_file_name( basename( $new_file_path ) ), 'post_content' => '', 'post_status' => 'inherit', ); // Insert the file into the Media Gallery $attachment_id = wp_insert_attachment( $attachment, $new_file_path ); // Verify if the insertion was successful if ( is_wp_error( $attachment_id ) ) { return $this->handle_response( $attachment_id ); } // Generate the attachment metadata require_once ABSPATH . 'wp-admin/includes/image.php'; $attachment_data = wp_generate_attachment_metadata( $attachment_id, $new_file_path ); wp_update_attachment_metadata( $attachment_id, $attachment_data ); return $this->handle_response( array( 'attachment_id' => $attachment_id ) ); } catch ( \Throwable $error ) { return $this->handle_response( array( 'code' => $error->getCode(), 'message' => $error->getMessage(), ) ); } } public static function get_rest_method() { return \WP_REST_Server::READABLE; } public static function get_rest_args() { return array(); } }