Sample code for 30+ languages & platforms
AutoIt

WordPress Media Upload

See more WordPress Examples

Demonstrates how to upload a media file to WordPress.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  This example requires the Chilkat API to have been previously unlocked.
;  See Global Unlock Sample for sample code.

$oHttp = ObjCreate("Chilkat.Http")

;  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.
$oHttp.Login = "wp_username"
;  Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
$oHttp.Password = "app_password"
$oHttp.BasicAuth = True

$oReq = ObjCreate("Chilkat.HttpRequest")
$oReq.HttpVerb = "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 

$oReq.ContentType = "image/jpg"
$oReq.Path = "/wp-json/wp/v2/media"

$bSuccess = $oReq.LoadBodyFromFile("qa_data/jpg/starfish.jpg")
If ($bSuccess = False) Then
    ConsoleWrite($oReq.LastErrorText & @CRLF)
    Exit
EndIf

$oReq.AddHeader "Content-Disposition","attachment; filename=starfish.jpg"

$oReq.AddHeader "Expect","100-continue"

$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpSReq("www.yourserver.com",443,True,$oReq,$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("HTTP response status: " & $oResp.StatusCode & @CRLF)

$oJsonResponse = ObjCreate("Chilkat.JsonObject")
$oJsonResponse.Load($oResp.BodyStr)

$oJsonResponse.EmitCompact = False
ConsoleWrite($oJsonResponse.Emit() & @CRLF)

;  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

$oDate_gmt = ObjCreate("Chilkat.DtObj")
Local $sHref
Local $bEmbeddable
Local $sName
Local $bTemplated

Local $id = $oJsonResponse.IntOf("id")
Local $sDate = $oJsonResponse.StringOf("date")
$oJsonResponse.DtOf("date_gmt",False,$oDate_gmt)
Local $sGuidRendered = $oJsonResponse.StringOf("guid.rendered")
Local $sGuidRaw = $oJsonResponse.StringOf("guid.raw")
Local $sModified = $oJsonResponse.StringOf("modified")
Local $sModified_gmt = $oJsonResponse.StringOf("modified_gmt")
Local $slug = $oJsonResponse.StringOf("slug")
Local $status = $oJsonResponse.StringOf("status")
Local $sV_type = $oJsonResponse.StringOf("type")
Local $sLink = $oJsonResponse.StringOf("link")
Local $sTitleRaw = $oJsonResponse.StringOf("title.raw")
Local $sTitleRendered = $oJsonResponse.StringOf("title.rendered")
Local $iAuthor = $oJsonResponse.IntOf("author")
Local $sComment_status = $oJsonResponse.StringOf("comment_status")
Local $sPing_status = $oJsonResponse.StringOf("ping_status")
Local $sTemplate = $oJsonResponse.StringOf("template")
Local $sPermalink_template = $oJsonResponse.StringOf("permalink_template")
Local $sGenerated_slug = $oJsonResponse.StringOf("generated_slug")
Local $sDescriptionRaw = $oJsonResponse.StringOf("description.raw")
Local $sDescriptionRendered = $oJsonResponse.StringOf("description.rendered")
Local $sCaptionRaw = $oJsonResponse.StringOf("caption.raw")
Local $sCaptionRendered = $oJsonResponse.StringOf("caption.rendered")
Local $sAlt_text = $oJsonResponse.StringOf("alt_text")
Local $sMedia_type = $oJsonResponse.StringOf("media_type")
Local $sMime_type = $oJsonResponse.StringOf("mime_type")
Local $iMedia_detailsWidth = $oJsonResponse.IntOf("media_details.width")
Local $iMedia_detailsHeight = $oJsonResponse.IntOf("media_details.height")
Local $sMedia_detailsFile = $oJsonResponse.StringOf("media_details.file")
Local $sMedia_detailsSizesWoocommerce_gallery_thumbnailFile = $oJsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.file")
Local $iMedia_detailsSizesWoocommerce_gallery_thumbnailWidth = $oJsonResponse.IntOf("media_details.sizes.woocommerce_gallery_thumbnail.width")
Local $iMedia_detailsSizesWoocommerce_gallery_thumbnailHeight = $oJsonResponse.IntOf("media_details.sizes.woocommerce_gallery_thumbnail.height")
Local $sMedia_detailsSizesWoocommerce_gallery_thumbnailMime_type = $oJsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.mime_type")
Local $sMedia_detailsSizesWoocommerce_gallery_thumbnailSource_url = $oJsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.source_url")
Local $sMedia_detailsSizesShop_thumbnailFile = $oJsonResponse.StringOf("media_details.sizes.shop_thumbnail.file")
Local $iMedia_detailsSizesShop_thumbnailWidth = $oJsonResponse.IntOf("media_details.sizes.shop_thumbnail.width")
Local $iMedia_detailsSizesShop_thumbnailHeight = $oJsonResponse.IntOf("media_details.sizes.shop_thumbnail.height")
Local $sMedia_detailsSizesShop_thumbnailMime_type = $oJsonResponse.StringOf("media_details.sizes.shop_thumbnail.mime_type")
Local $sMedia_detailsSizesShop_thumbnailSource_url = $oJsonResponse.StringOf("media_details.sizes.shop_thumbnail.source_url")
Local $sMedia_detailsSizesFullFile = $oJsonResponse.StringOf("media_details.sizes.full.file")
Local $iMedia_detailsSizesFullWidth = $oJsonResponse.IntOf("media_details.sizes.full.width")
Local $iMedia_detailsSizesFullHeight = $oJsonResponse.IntOf("media_details.sizes.full.height")
Local $sMedia_detailsSizesFullMime_type = $oJsonResponse.StringOf("media_details.sizes.full.mime_type")
Local $sMedia_detailsSizesFullSource_url = $oJsonResponse.StringOf("media_details.sizes.full.source_url")
Local $sMedia_detailsImage_metaAperture = $oJsonResponse.StringOf("media_details.image_meta.aperture")
Local $sMedia_detailsImage_metaCredit = $oJsonResponse.StringOf("media_details.image_meta.credit")
Local $sMedia_detailsImage_metaCamera = $oJsonResponse.StringOf("media_details.image_meta.camera")
Local $sMedia_detailsImage_metaCaption = $oJsonResponse.StringOf("media_details.image_meta.caption")
Local $sMedia_detailsImage_metaCreated_timestamp = $oJsonResponse.StringOf("media_details.image_meta.created_timestamp")
Local $sMedia_detailsImage_metaCopyright = $oJsonResponse.StringOf("media_details.image_meta.copyright")
Local $sMedia_detailsImage_metaFocal_length = $oJsonResponse.StringOf("media_details.image_meta.focal_length")
Local $sMedia_detailsImage_metaIso = $oJsonResponse.StringOf("media_details.image_meta.iso")
Local $sMedia_detailsImage_metaShutter_speed = $oJsonResponse.StringOf("media_details.image_meta.shutter_speed")
Local $sMedia_detailsImage_metaTitle = $oJsonResponse.StringOf("media_details.image_meta.title")
Local $sMedia_detailsImage_metaOrientation = $oJsonResponse.StringOf("media_details.image_meta.orientation")
Local $sPost = $oJsonResponse.StringOf("post")
Local $source_url = $oJsonResponse.StringOf("source_url")
Local $i = 0
Local $iCount_i = $oJsonResponse.SizeOfArray("meta")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("media_details.image_meta.keywords")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("missing_image_sizes")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.self")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $sHref = $oJsonResponse.StringOf("_links.self[i].href")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.collection")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $sHref = $oJsonResponse.StringOf("_links.collection[i].href")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.about")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $sHref = $oJsonResponse.StringOf("_links.about[i].href")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.author")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $bEmbeddable = $oJsonResponse.BoolOf("_links.author[i].embeddable")
    $sHref = $oJsonResponse.StringOf("_links.author[i].href")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.replies")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $bEmbeddable = $oJsonResponse.BoolOf("_links.replies[i].embeddable")
    $sHref = $oJsonResponse.StringOf("_links.replies[i].href")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.wp:action-unfiltered-html")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $sHref = $oJsonResponse.StringOf("_links.wp:action-unfiltered-html[i].href")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.wp:action-assign-author")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $sHref = $oJsonResponse.StringOf("_links.wp:action-assign-author[i].href")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonResponse.SizeOfArray("_links.curies")
While $i < $iCount_i
    $oJsonResponse.I = $i
    $sName = $oJsonResponse.StringOf("_links.curies[i].name")
    $sHref = $oJsonResponse.StringOf("_links.curies[i].href")
    $bTemplated = $oJsonResponse.BoolOf("_links.curies[i].templated")
    $i = $i + 1
Wend