Sample code for 30+ languages & platforms
Zig

WordPress Media Upload

See more WordPress Examples

Demonstrates how to upload a media file to WordPress.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

    // 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.setLogin("wp_username");
    // Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
    http.setPassword("app_password");
    http.setBasicAuth(true);

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("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.setContentType("image/jpg");
    req.setPath("/wp-json/wp/v2/media");

    req.loadBodyFromFile("qa_data/jpg/starfish.jpg") catch {
        std.debug.print("{s}\n", .{try req.getLastErrorText(alloc)});
        return;
    };

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

    req.addHeader("Expect", "100-continue");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("www.yourserver.com", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("HTTP response status: {d}\n", .{resp.getStatusCode()});

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(try resp.getBodyStr(alloc)) catch {};

    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    // 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

    const date_gmt = try chilkat.DtObj.init();
    defer date_gmt.deinit();
    var href: [:0]const u8 = "";
    var embeddable: bool = false;
    var name: [:0]const u8 = "";
    var templated: bool = false;

    _ = json_response.intOf("id");
    _ = try json_response.stringOf(alloc, "date");
    json_response.dtOf("date_gmt", false, date_gmt) catch {};
    _ = try json_response.stringOf(alloc, "guid.rendered");
    _ = try json_response.stringOf(alloc, "guid.raw");
    _ = try json_response.stringOf(alloc, "modified");
    _ = try json_response.stringOf(alloc, "modified_gmt");
    _ = try json_response.stringOf(alloc, "slug");
    _ = try json_response.stringOf(alloc, "status");
    _ = try json_response.stringOf(alloc, "type");
    _ = try json_response.stringOf(alloc, "link");
    _ = try json_response.stringOf(alloc, "title.raw");
    _ = try json_response.stringOf(alloc, "title.rendered");
    _ = json_response.intOf("author");
    _ = try json_response.stringOf(alloc, "comment_status");
    _ = try json_response.stringOf(alloc, "ping_status");
    _ = try json_response.stringOf(alloc, "template");
    _ = try json_response.stringOf(alloc, "permalink_template");
    _ = try json_response.stringOf(alloc, "generated_slug");
    _ = try json_response.stringOf(alloc, "description.raw");
    _ = try json_response.stringOf(alloc, "description.rendered");
    _ = try json_response.stringOf(alloc, "caption.raw");
    _ = try json_response.stringOf(alloc, "caption.rendered");
    _ = try json_response.stringOf(alloc, "alt_text");
    _ = try json_response.stringOf(alloc, "media_type");
    _ = try json_response.stringOf(alloc, "mime_type");
    _ = json_response.intOf("media_details.width");
    _ = json_response.intOf("media_details.height");
    _ = try json_response.stringOf(alloc, "media_details.file");
    _ = try json_response.stringOf(alloc, "media_details.sizes.woocommerce_gallery_thumbnail.file");
    _ = json_response.intOf("media_details.sizes.woocommerce_gallery_thumbnail.width");
    _ = json_response.intOf("media_details.sizes.woocommerce_gallery_thumbnail.height");
    _ = try json_response.stringOf(alloc, "media_details.sizes.woocommerce_gallery_thumbnail.mime_type");
    _ = try json_response.stringOf(alloc, "media_details.sizes.woocommerce_gallery_thumbnail.source_url");
    _ = try json_response.stringOf(alloc, "media_details.sizes.shop_thumbnail.file");
    _ = json_response.intOf("media_details.sizes.shop_thumbnail.width");
    _ = json_response.intOf("media_details.sizes.shop_thumbnail.height");
    _ = try json_response.stringOf(alloc, "media_details.sizes.shop_thumbnail.mime_type");
    _ = try json_response.stringOf(alloc, "media_details.sizes.shop_thumbnail.source_url");
    _ = try json_response.stringOf(alloc, "media_details.sizes.full.file");
    _ = json_response.intOf("media_details.sizes.full.width");
    _ = json_response.intOf("media_details.sizes.full.height");
    _ = try json_response.stringOf(alloc, "media_details.sizes.full.mime_type");
    _ = try json_response.stringOf(alloc, "media_details.sizes.full.source_url");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.aperture");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.credit");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.camera");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.caption");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.created_timestamp");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.copyright");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.focal_length");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.iso");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.shutter_speed");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.title");
    _ = try json_response.stringOf(alloc, "media_details.image_meta.orientation");
    _ = try json_response.stringOf(alloc, "post");
    _ = try json_response.stringOf(alloc, "source_url");
    var i: i32 = 0;
    var count_i: i32 = json_response.sizeOfArray("meta");
    while (i < count_i) {
        json_response.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("media_details.image_meta.keywords");
    while (i < count_i) {
        json_response.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("missing_image_sizes");
    while (i < count_i) {
        json_response.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.self");
    while (i < count_i) {
        json_response.setI(i);
        href = try json_response.stringOf(alloc, "_links.self[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.collection");
    while (i < count_i) {
        json_response.setI(i);
        href = try json_response.stringOf(alloc, "_links.collection[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.about");
    while (i < count_i) {
        json_response.setI(i);
        href = try json_response.stringOf(alloc, "_links.about[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.author");
    while (i < count_i) {
        json_response.setI(i);
        embeddable = json_response.boolOf("_links.author[i].embeddable");
        href = try json_response.stringOf(alloc, "_links.author[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.replies");
    while (i < count_i) {
        json_response.setI(i);
        embeddable = json_response.boolOf("_links.replies[i].embeddable");
        href = try json_response.stringOf(alloc, "_links.replies[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.wp:action-unfiltered-html");
    while (i < count_i) {
        json_response.setI(i);
        href = try json_response.stringOf(alloc, "_links.wp:action-unfiltered-html[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.wp:action-assign-author");
    while (i < count_i) {
        json_response.setI(i);
        href = try json_response.stringOf(alloc, "_links.wp:action-assign-author[i].href");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("_links.curies");
    while (i < count_i) {
        json_response.setI(i);
        name = try json_response.stringOf(alloc, "_links.curies[i].name");
        href = try json_response.stringOf(alloc, "_links.curies[i].href");
        templated = json_response.boolOf("_links.curies[i].templated");
        i = i + 1;
    }
}