Sample code for 30+ languages & platforms
Unicode C++

WordPress Media Upload

See more WordPress Examples

Demonstrates how to upload a media file to WordPress.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkDtObjW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkHttpW 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.
    http.put_Login(L"wp_username");
    //  Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
    http.put_Password(L"app_password");
    http.put_BasicAuth(true);

    CkHttpRequestW req;
    req.put_HttpVerb(L"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.put_ContentType(L"image/jpg");
    req.put_Path(L"/wp-json/wp/v2/media");

    success = req.LoadBodyFromFile(L"qa_data/jpg/starfish.jpg");
    if (success == false) {
        wprintf(L"%s\n",req.lastErrorText());
        return;
    }

    req.AddHeader(L"Content-Disposition",L"attachment; filename=starfish.jpg");

    req.AddHeader(L"Expect",L"100-continue");

    CkHttpResponseW resp;
    success = http.HttpSReq(L"www.yourserver.com",443,true,req,resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"HTTP response status: %d\n",resp.get_StatusCode());

    CkJsonObjectW jsonResponse;
    jsonResponse.Load(resp.bodyStr());

    jsonResponse.put_EmitCompact(false);
    wprintf(L"%s\n",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

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    CkDtObjW date_gmt;
    const wchar_t *href = 0;
    bool embeddable;
    const wchar_t *name = 0;
    bool templated;

    int id = jsonResponse.IntOf(L"id");
    const wchar_t *date = jsonResponse.stringOf(L"date");
    jsonResponse.DtOf(L"date_gmt",false,date_gmt);
    const wchar_t *guidRendered = jsonResponse.stringOf(L"guid.rendered");
    const wchar_t *guidRaw = jsonResponse.stringOf(L"guid.raw");
    const wchar_t *modified = jsonResponse.stringOf(L"modified");
    const wchar_t *modified_gmt = jsonResponse.stringOf(L"modified_gmt");
    const wchar_t *slug = jsonResponse.stringOf(L"slug");
    const wchar_t *status = jsonResponse.stringOf(L"status");
    const wchar_t *v_type = jsonResponse.stringOf(L"type");
    const wchar_t *link = jsonResponse.stringOf(L"link");
    const wchar_t *titleRaw = jsonResponse.stringOf(L"title.raw");
    const wchar_t *titleRendered = jsonResponse.stringOf(L"title.rendered");
    int author = jsonResponse.IntOf(L"author");
    const wchar_t *comment_status = jsonResponse.stringOf(L"comment_status");
    const wchar_t *ping_status = jsonResponse.stringOf(L"ping_status");
    const wchar_t *template = jsonResponse.stringOf(L"template");
    const wchar_t *permalink_template = jsonResponse.stringOf(L"permalink_template");
    const wchar_t *generated_slug = jsonResponse.stringOf(L"generated_slug");
    const wchar_t *descriptionRaw = jsonResponse.stringOf(L"description.raw");
    const wchar_t *descriptionRendered = jsonResponse.stringOf(L"description.rendered");
    const wchar_t *captionRaw = jsonResponse.stringOf(L"caption.raw");
    const wchar_t *captionRendered = jsonResponse.stringOf(L"caption.rendered");
    const wchar_t *alt_text = jsonResponse.stringOf(L"alt_text");
    const wchar_t *media_type = jsonResponse.stringOf(L"media_type");
    const wchar_t *mime_type = jsonResponse.stringOf(L"mime_type");
    int media_detailsWidth = jsonResponse.IntOf(L"media_details.width");
    int media_detailsHeight = jsonResponse.IntOf(L"media_details.height");
    const wchar_t *media_detailsFile = jsonResponse.stringOf(L"media_details.file");
    const wchar_t *media_detailsSizesWoocommerce_gallery_thumbnailFile = jsonResponse.stringOf(L"media_details.sizes.woocommerce_gallery_thumbnail.file");
    int media_detailsSizesWoocommerce_gallery_thumbnailWidth = jsonResponse.IntOf(L"media_details.sizes.woocommerce_gallery_thumbnail.width");
    int media_detailsSizesWoocommerce_gallery_thumbnailHeight = jsonResponse.IntOf(L"media_details.sizes.woocommerce_gallery_thumbnail.height");
    const wchar_t *media_detailsSizesWoocommerce_gallery_thumbnailMime_type = jsonResponse.stringOf(L"media_details.sizes.woocommerce_gallery_thumbnail.mime_type");
    const wchar_t *media_detailsSizesWoocommerce_gallery_thumbnailSource_url = jsonResponse.stringOf(L"media_details.sizes.woocommerce_gallery_thumbnail.source_url");
    const wchar_t *media_detailsSizesShop_thumbnailFile = jsonResponse.stringOf(L"media_details.sizes.shop_thumbnail.file");
    int media_detailsSizesShop_thumbnailWidth = jsonResponse.IntOf(L"media_details.sizes.shop_thumbnail.width");
    int media_detailsSizesShop_thumbnailHeight = jsonResponse.IntOf(L"media_details.sizes.shop_thumbnail.height");
    const wchar_t *media_detailsSizesShop_thumbnailMime_type = jsonResponse.stringOf(L"media_details.sizes.shop_thumbnail.mime_type");
    const wchar_t *media_detailsSizesShop_thumbnailSource_url = jsonResponse.stringOf(L"media_details.sizes.shop_thumbnail.source_url");
    const wchar_t *media_detailsSizesFullFile = jsonResponse.stringOf(L"media_details.sizes.full.file");
    int media_detailsSizesFullWidth = jsonResponse.IntOf(L"media_details.sizes.full.width");
    int media_detailsSizesFullHeight = jsonResponse.IntOf(L"media_details.sizes.full.height");
    const wchar_t *media_detailsSizesFullMime_type = jsonResponse.stringOf(L"media_details.sizes.full.mime_type");
    const wchar_t *media_detailsSizesFullSource_url = jsonResponse.stringOf(L"media_details.sizes.full.source_url");
    const wchar_t *media_detailsImage_metaAperture = jsonResponse.stringOf(L"media_details.image_meta.aperture");
    const wchar_t *media_detailsImage_metaCredit = jsonResponse.stringOf(L"media_details.image_meta.credit");
    const wchar_t *media_detailsImage_metaCamera = jsonResponse.stringOf(L"media_details.image_meta.camera");
    const wchar_t *media_detailsImage_metaCaption = jsonResponse.stringOf(L"media_details.image_meta.caption");
    const wchar_t *media_detailsImage_metaCreated_timestamp = jsonResponse.stringOf(L"media_details.image_meta.created_timestamp");
    const wchar_t *media_detailsImage_metaCopyright = jsonResponse.stringOf(L"media_details.image_meta.copyright");
    const wchar_t *media_detailsImage_metaFocal_length = jsonResponse.stringOf(L"media_details.image_meta.focal_length");
    const wchar_t *media_detailsImage_metaIso = jsonResponse.stringOf(L"media_details.image_meta.iso");
    const wchar_t *media_detailsImage_metaShutter_speed = jsonResponse.stringOf(L"media_details.image_meta.shutter_speed");
    const wchar_t *media_detailsImage_metaTitle = jsonResponse.stringOf(L"media_details.image_meta.title");
    const wchar_t *media_detailsImage_metaOrientation = jsonResponse.stringOf(L"media_details.image_meta.orientation");
    const wchar_t *post = jsonResponse.stringOf(L"post");
    const wchar_t *source_url = jsonResponse.stringOf(L"source_url");
    int i = 0;
    int count_i = jsonResponse.SizeOfArray(L"meta");
    while (i < count_i) {
        jsonResponse.put_I(i);
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"media_details.image_meta.keywords");
    while (i < count_i) {
        jsonResponse.put_I(i);
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"missing_image_sizes");
    while (i < count_i) {
        jsonResponse.put_I(i);
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.self");
    while (i < count_i) {
        jsonResponse.put_I(i);
        href = jsonResponse.stringOf(L"_links.self[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.collection");
    while (i < count_i) {
        jsonResponse.put_I(i);
        href = jsonResponse.stringOf(L"_links.collection[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.about");
    while (i < count_i) {
        jsonResponse.put_I(i);
        href = jsonResponse.stringOf(L"_links.about[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.author");
    while (i < count_i) {
        jsonResponse.put_I(i);
        embeddable = jsonResponse.BoolOf(L"_links.author[i].embeddable");
        href = jsonResponse.stringOf(L"_links.author[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.replies");
    while (i < count_i) {
        jsonResponse.put_I(i);
        embeddable = jsonResponse.BoolOf(L"_links.replies[i].embeddable");
        href = jsonResponse.stringOf(L"_links.replies[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.wp:action-unfiltered-html");
    while (i < count_i) {
        jsonResponse.put_I(i);
        href = jsonResponse.stringOf(L"_links.wp:action-unfiltered-html[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.wp:action-assign-author");
    while (i < count_i) {
        jsonResponse.put_I(i);
        href = jsonResponse.stringOf(L"_links.wp:action-assign-author[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"_links.curies");
    while (i < count_i) {
        jsonResponse.put_I(i);
        name = jsonResponse.stringOf(L"_links.curies[i].name");
        href = jsonResponse.stringOf(L"_links.curies[i].href");
        templated = jsonResponse.BoolOf(L"_links.curies[i].templated");
        i = i + 1;
    }
    }