Lua
Lua
WordPress Media Upload
See more WordPress Examples
Demonstrates how to upload a media file to WordPress.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- This example will use WordPress username / Application password authentication using the "Applications Password" plugin.
-- See https://wordpress.org/plugins/application-passwords/
-- Use your WordPress login, such as "admin", not the application name.
http.login = "wp_username"
-- Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
http.password = "app_password"
http.basic_auth = true
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
-- Use the Content-Type that corresponds to the type of file you are uploading.
-- WordPress supports uploading the following file types:
-- Images
--
-- .jpg (image/jpg)
-- .jpeg (image/jpeg)
-- .png (image/png)
-- .gif (image/gif)
-- .ico (image/x-icon)
--
-- Documents
--
-- .pdf (applicatin/pdf)
-- .doc, .docx (application/msword)
-- .ppt, .pptx, .pps, .ppsx (application/mspowerpoint)
-- .odt
-- .xls, .xlsx (application/msexcel)
-- .psd (??? image/vnd.adobe.photoshop,application/x-photoshop,application/photoshop,application/psd,image/psd)
--
-- Audio
--
-- .mp3 (audio/mpeg)
-- .m4a
-- .ogg
-- .wav (audio/wav)
--
-- Video
--
-- .mp4, .m4v (video/mp4)
-- .mov (video/quicktime)
-- .wmv (video/x-ms-wmv)
-- .avi (video/avi)
-- .mpg (video/mpeg)
-- .ogv
-- .3gp
-- .3g2
req.content_type = "image/jpg"
req.path = "/wp-json/wp/v2/media"
local success = req:load_body_from_file("qa_data/jpg/starfish.jpg")
if not success then
print(req.last_error_text)
return
end
req:add_header("Content-Disposition", "attachment; filename=starfish.jpg")
req:add_header("Expect", "100-continue")
local resp = chilkat.HttpResponse.new()
success = http:http_sreq("www.yourserver.com", 443, true, req, resp)
if not success then
print(http.last_error_text)
return
end
print("HTTP response status: " .. resp.status_code)
local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load(resp.body_str)
jsonResponse.emit_compact = false
print(jsonResponse:emit())
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "id": 1915,
-- "date": "2021-01-19T18:25:01",
-- "date_gmt": "2021-01-20T01:25:01",
-- "guid": {
-- "rendered": "http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg",
-- "raw": "http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg"
-- },
-- "modified": "2021-01-19T18:25:01",
-- "modified_gmt": "2021-01-20T01:25:01",
-- "slug": "starfish",
-- "status": "inherit",
-- "type": "attachment",
-- "link": "https:\/\/cknotes.com\/starfish\/",
-- "title": {
-- "raw": "starfish",
-- "rendered": "starfish"
-- },
-- "author": 1,
-- "comment_status": "closed",
-- "ping_status": "closed",
-- "template": "",
-- "meta": [
-- ],
-- "permalink_template": "https:\/\/cknotes.com\/?attachment_id=1915",
-- "generated_slug": "starfish",
-- "description": {
-- "raw": "",
-- "rendered": "<p class=\"attachment\"><a href='https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg'><img width=\"120\" height=\"120\" src=\"https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg\" class=\"attachment-medium size-medium\" alt=\"\" loading=\"lazy\" srcset=\"http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg 120w, http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg 100w\" sizes=\"(max-width: 120px) 100vw, 120px\" \/><\/a><\/p>\n"
-- },
-- "caption": {
-- "raw": "",
-- "rendered": ""
-- },
-- "alt_text": "",
-- "media_type": "image",
-- "mime_type": "image\/jpeg",
-- "media_details": {
-- "width": 120,
-- "height": 120,
-- "file": "2021\/01\/starfish.jpg",
-- "sizes": {
-- "woocommerce_gallery_thumbnail": {
-- "file": "starfish-100x100.jpg",
-- "width": 100,
-- "height": 100,
-- "mime_type": "image\/jpeg",
-- "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg"
-- },
-- "shop_thumbnail": {
-- "file": "starfish-100x100.jpg",
-- "width": 100,
-- "height": 100,
-- "mime_type": "image\/jpeg",
-- "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg"
-- },
-- "full": {
-- "file": "starfish.jpg",
-- "width": 120,
-- "height": 120,
-- "mime_type": "image\/jpeg",
-- "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg"
-- }
-- },
-- "image_meta": {
-- "aperture": "0",
-- "credit": "",
-- "camera": "",
-- "caption": "",
-- "created_timestamp": "0",
-- "copyright": "",
-- "focal_length": "0",
-- "iso": "0",
-- "shutter_speed": "0",
-- "title": "",
-- "orientation": "0",
-- "keywords": [
-- ]
-- }
-- },
-- "post": null,
-- "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg",
-- "missing_image_sizes": [
-- ],
-- "_links": {
-- "self": [
-- {
-- "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
-- }
-- ],
-- "collection": [
-- {
-- "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media"
-- }
-- ],
-- "about": [
-- {
-- "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/attachment"
-- }
-- ],
-- "author": [
-- {
-- "embeddable": true,
-- "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"
-- }
-- ],
-- "replies": [
-- {
-- "embeddable": true,
-- "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=1915"
-- }
-- ],
-- "wp:action-unfiltered-html": [
-- {
-- "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
-- }
-- ],
-- "wp:action-assign-author": [
-- {
-- "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
-- }
-- ],
-- "curies": [
-- {
-- "name": "wp",
-- "href": "https:\/\/api.w.org\/{rel}",
-- "templated": true
-- }
-- ]
-- }
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local date_gmt = chilkat.DtObj.new()
local href = ""
local embeddable = false
local name = ""
local templated = false
local id = jsonResponse:int_of("id")
local date = jsonResponse:string_of("date")
jsonResponse:dt_of("date_gmt", false, date_gmt)
local guidRendered = jsonResponse:string_of("guid.rendered")
local guidRaw = jsonResponse:string_of("guid.raw")
local modified = jsonResponse:string_of("modified")
local modified_gmt = jsonResponse:string_of("modified_gmt")
local slug = jsonResponse:string_of("slug")
local status = jsonResponse:string_of("status")
local v_type = jsonResponse:string_of("type")
local link = jsonResponse:string_of("link")
local titleRaw = jsonResponse:string_of("title.raw")
local titleRendered = jsonResponse:string_of("title.rendered")
local author = jsonResponse:int_of("author")
local comment_status = jsonResponse:string_of("comment_status")
local ping_status = jsonResponse:string_of("ping_status")
local template = jsonResponse:string_of("template")
local permalink_template = jsonResponse:string_of("permalink_template")
local generated_slug = jsonResponse:string_of("generated_slug")
local descriptionRaw = jsonResponse:string_of("description.raw")
local descriptionRendered = jsonResponse:string_of("description.rendered")
local captionRaw = jsonResponse:string_of("caption.raw")
local captionRendered = jsonResponse:string_of("caption.rendered")
local alt_text = jsonResponse:string_of("alt_text")
local media_type = jsonResponse:string_of("media_type")
local mime_type = jsonResponse:string_of("mime_type")
local media_detailsWidth = jsonResponse:int_of("media_details.width")
local media_detailsHeight = jsonResponse:int_of("media_details.height")
local media_detailsFile = jsonResponse:string_of("media_details.file")
local media_detailsSizesWoocommerce_gallery_thumbnailFile = jsonResponse:string_of("media_details.sizes.woocommerce_gallery_thumbnail.file")
local media_detailsSizesWoocommerce_gallery_thumbnailWidth = jsonResponse:int_of("media_details.sizes.woocommerce_gallery_thumbnail.width")
local media_detailsSizesWoocommerce_gallery_thumbnailHeight = jsonResponse:int_of("media_details.sizes.woocommerce_gallery_thumbnail.height")
local media_detailsSizesWoocommerce_gallery_thumbnailMime_type = jsonResponse:string_of("media_details.sizes.woocommerce_gallery_thumbnail.mime_type")
local media_detailsSizesWoocommerce_gallery_thumbnailSource_url = jsonResponse:string_of("media_details.sizes.woocommerce_gallery_thumbnail.source_url")
local media_detailsSizesShop_thumbnailFile = jsonResponse:string_of("media_details.sizes.shop_thumbnail.file")
local media_detailsSizesShop_thumbnailWidth = jsonResponse:int_of("media_details.sizes.shop_thumbnail.width")
local media_detailsSizesShop_thumbnailHeight = jsonResponse:int_of("media_details.sizes.shop_thumbnail.height")
local media_detailsSizesShop_thumbnailMime_type = jsonResponse:string_of("media_details.sizes.shop_thumbnail.mime_type")
local media_detailsSizesShop_thumbnailSource_url = jsonResponse:string_of("media_details.sizes.shop_thumbnail.source_url")
local media_detailsSizesFullFile = jsonResponse:string_of("media_details.sizes.full.file")
local media_detailsSizesFullWidth = jsonResponse:int_of("media_details.sizes.full.width")
local media_detailsSizesFullHeight = jsonResponse:int_of("media_details.sizes.full.height")
local media_detailsSizesFullMime_type = jsonResponse:string_of("media_details.sizes.full.mime_type")
local media_detailsSizesFullSource_url = jsonResponse:string_of("media_details.sizes.full.source_url")
local media_detailsImage_metaAperture = jsonResponse:string_of("media_details.image_meta.aperture")
local media_detailsImage_metaCredit = jsonResponse:string_of("media_details.image_meta.credit")
local media_detailsImage_metaCamera = jsonResponse:string_of("media_details.image_meta.camera")
local media_detailsImage_metaCaption = jsonResponse:string_of("media_details.image_meta.caption")
local media_detailsImage_metaCreated_timestamp = jsonResponse:string_of("media_details.image_meta.created_timestamp")
local media_detailsImage_metaCopyright = jsonResponse:string_of("media_details.image_meta.copyright")
local media_detailsImage_metaFocal_length = jsonResponse:string_of("media_details.image_meta.focal_length")
local media_detailsImage_metaIso = jsonResponse:string_of("media_details.image_meta.iso")
local media_detailsImage_metaShutter_speed = jsonResponse:string_of("media_details.image_meta.shutter_speed")
local media_detailsImage_metaTitle = jsonResponse:string_of("media_details.image_meta.title")
local media_detailsImage_metaOrientation = jsonResponse:string_of("media_details.image_meta.orientation")
local post = jsonResponse:string_of("post")
local source_url = jsonResponse:string_of("source_url")
local i = 0
local count_i = jsonResponse:size_of_array("meta")
while i < count_i do
jsonResponse.i = i
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("media_details.image_meta.keywords")
while i < count_i do
jsonResponse.i = i
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("missing_image_sizes")
while i < count_i do
jsonResponse.i = i
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.self")
while i < count_i do
jsonResponse.i = i
href = jsonResponse:string_of("_links.self[i].href")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.collection")
while i < count_i do
jsonResponse.i = i
href = jsonResponse:string_of("_links.collection[i].href")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.about")
while i < count_i do
jsonResponse.i = i
href = jsonResponse:string_of("_links.about[i].href")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.author")
while i < count_i do
jsonResponse.i = i
embeddable = jsonResponse:bool_of("_links.author[i].embeddable")
href = jsonResponse:string_of("_links.author[i].href")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.replies")
while i < count_i do
jsonResponse.i = i
embeddable = jsonResponse:bool_of("_links.replies[i].embeddable")
href = jsonResponse:string_of("_links.replies[i].href")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.wp:action-unfiltered-html")
while i < count_i do
jsonResponse.i = i
href = jsonResponse:string_of("_links.wp:action-unfiltered-html[i].href")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.wp:action-assign-author")
while i < count_i do
jsonResponse.i = i
href = jsonResponse:string_of("_links.wp:action-assign-author[i].href")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("_links.curies")
while i < count_i do
jsonResponse.i = i
name = jsonResponse:string_of("_links.curies[i].name")
href = jsonResponse:string_of("_links.curies[i].href")
templated = jsonResponse:bool_of("_links.curies[i].templated")
i = i + 1
end