Objective-C
Objective-C
WordPress Media Upload
See more WordPress Examples
Demonstrates how to upload a media file to WordPress.Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpRequest.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
#import <CkoDtObj.h>
#import <NSString.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
// 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.BasicAuth = YES;
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
req.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
req.ContentType = @"image/jpg";
req.Path = @"/wp-json/wp/v2/media";
success = [req LoadBodyFromFile: @"qa_data/jpg/starfish.jpg"];
if (success == NO) {
NSLog(@"%@",req.LastErrorText);
return;
}
[req AddHeader: @"Content-Disposition" value: @"attachment; filename=starfish.jpg"];
[req AddHeader: @"Expect" value: @"100-continue"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpSReq: @"www.yourserver.com" port: [NSNumber numberWithInt: 443] ssl: YES request: req response: resp];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
NSLog(@"%@%d",@"HTTP response status: ",[resp.StatusCode intValue]);
CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse Load: resp.BodyStr];
jsonResponse.EmitCompact = NO;
NSLog(@"%@",[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
CkoDtObj *date_gmt = [[CkoDtObj alloc] init];
NSString *href = 0;
BOOL embeddable;
NSString *name = 0;
BOOL templated;
int id = [[jsonResponse IntOf: @"id"] intValue];
NSString *date = [jsonResponse StringOf: @"date"];
[jsonResponse DtOf: @"date_gmt" bLocal: NO dt: date_gmt];
NSString *guidRendered = [jsonResponse StringOf: @"guid.rendered"];
NSString *guidRaw = [jsonResponse StringOf: @"guid.raw"];
NSString *modified = [jsonResponse StringOf: @"modified"];
NSString *modified_gmt = [jsonResponse StringOf: @"modified_gmt"];
NSString *slug = [jsonResponse StringOf: @"slug"];
NSString *status = [jsonResponse StringOf: @"status"];
NSString *v_type = [jsonResponse StringOf: @"type"];
NSString *link = [jsonResponse StringOf: @"link"];
NSString *titleRaw = [jsonResponse StringOf: @"title.raw"];
NSString *titleRendered = [jsonResponse StringOf: @"title.rendered"];
int author = [[jsonResponse IntOf: @"author"] intValue];
NSString *comment_status = [jsonResponse StringOf: @"comment_status"];
NSString *ping_status = [jsonResponse StringOf: @"ping_status"];
NSString *template = [jsonResponse StringOf: @"template"];
NSString *permalink_template = [jsonResponse StringOf: @"permalink_template"];
NSString *generated_slug = [jsonResponse StringOf: @"generated_slug"];
NSString *descriptionRaw = [jsonResponse StringOf: @"description.raw"];
NSString *descriptionRendered = [jsonResponse StringOf: @"description.rendered"];
NSString *captionRaw = [jsonResponse StringOf: @"caption.raw"];
NSString *captionRendered = [jsonResponse StringOf: @"caption.rendered"];
NSString *alt_text = [jsonResponse StringOf: @"alt_text"];
NSString *media_type = [jsonResponse StringOf: @"media_type"];
NSString *mime_type = [jsonResponse StringOf: @"mime_type"];
int media_detailsWidth = [[jsonResponse IntOf: @"media_details.width"] intValue];
int media_detailsHeight = [[jsonResponse IntOf: @"media_details.height"] intValue];
NSString *media_detailsFile = [jsonResponse StringOf: @"media_details.file"];
NSString *media_detailsSizesWoocommerce_gallery_thumbnailFile = [jsonResponse StringOf: @"media_details.sizes.woocommerce_gallery_thumbnail.file"];
int media_detailsSizesWoocommerce_gallery_thumbnailWidth = [[jsonResponse IntOf: @"media_details.sizes.woocommerce_gallery_thumbnail.width"] intValue];
int media_detailsSizesWoocommerce_gallery_thumbnailHeight = [[jsonResponse IntOf: @"media_details.sizes.woocommerce_gallery_thumbnail.height"] intValue];
NSString *media_detailsSizesWoocommerce_gallery_thumbnailMime_type = [jsonResponse StringOf: @"media_details.sizes.woocommerce_gallery_thumbnail.mime_type"];
NSString *media_detailsSizesWoocommerce_gallery_thumbnailSource_url = [jsonResponse StringOf: @"media_details.sizes.woocommerce_gallery_thumbnail.source_url"];
NSString *media_detailsSizesShop_thumbnailFile = [jsonResponse StringOf: @"media_details.sizes.shop_thumbnail.file"];
int media_detailsSizesShop_thumbnailWidth = [[jsonResponse IntOf: @"media_details.sizes.shop_thumbnail.width"] intValue];
int media_detailsSizesShop_thumbnailHeight = [[jsonResponse IntOf: @"media_details.sizes.shop_thumbnail.height"] intValue];
NSString *media_detailsSizesShop_thumbnailMime_type = [jsonResponse StringOf: @"media_details.sizes.shop_thumbnail.mime_type"];
NSString *media_detailsSizesShop_thumbnailSource_url = [jsonResponse StringOf: @"media_details.sizes.shop_thumbnail.source_url"];
NSString *media_detailsSizesFullFile = [jsonResponse StringOf: @"media_details.sizes.full.file"];
int media_detailsSizesFullWidth = [[jsonResponse IntOf: @"media_details.sizes.full.width"] intValue];
int media_detailsSizesFullHeight = [[jsonResponse IntOf: @"media_details.sizes.full.height"] intValue];
NSString *media_detailsSizesFullMime_type = [jsonResponse StringOf: @"media_details.sizes.full.mime_type"];
NSString *media_detailsSizesFullSource_url = [jsonResponse StringOf: @"media_details.sizes.full.source_url"];
NSString *media_detailsImage_metaAperture = [jsonResponse StringOf: @"media_details.image_meta.aperture"];
NSString *media_detailsImage_metaCredit = [jsonResponse StringOf: @"media_details.image_meta.credit"];
NSString *media_detailsImage_metaCamera = [jsonResponse StringOf: @"media_details.image_meta.camera"];
NSString *media_detailsImage_metaCaption = [jsonResponse StringOf: @"media_details.image_meta.caption"];
NSString *media_detailsImage_metaCreated_timestamp = [jsonResponse StringOf: @"media_details.image_meta.created_timestamp"];
NSString *media_detailsImage_metaCopyright = [jsonResponse StringOf: @"media_details.image_meta.copyright"];
NSString *media_detailsImage_metaFocal_length = [jsonResponse StringOf: @"media_details.image_meta.focal_length"];
NSString *media_detailsImage_metaIso = [jsonResponse StringOf: @"media_details.image_meta.iso"];
NSString *media_detailsImage_metaShutter_speed = [jsonResponse StringOf: @"media_details.image_meta.shutter_speed"];
NSString *media_detailsImage_metaTitle = [jsonResponse StringOf: @"media_details.image_meta.title"];
NSString *media_detailsImage_metaOrientation = [jsonResponse StringOf: @"media_details.image_meta.orientation"];
NSString *post = [jsonResponse StringOf: @"post"];
NSString *source_url = [jsonResponse StringOf: @"source_url"];
int i = 0;
int count_i = [[jsonResponse SizeOfArray: @"meta"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"media_details.image_meta.keywords"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"missing_image_sizes"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.self"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
href = [jsonResponse StringOf: @"_links.self[i].href"];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.collection"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
href = [jsonResponse StringOf: @"_links.collection[i].href"];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.about"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
href = [jsonResponse StringOf: @"_links.about[i].href"];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.author"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
embeddable = [jsonResponse BoolOf: @"_links.author[i].embeddable"];
href = [jsonResponse StringOf: @"_links.author[i].href"];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.replies"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
embeddable = [jsonResponse BoolOf: @"_links.replies[i].embeddable"];
href = [jsonResponse StringOf: @"_links.replies[i].href"];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.wp:action-unfiltered-html"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
href = [jsonResponse StringOf: @"_links.wp:action-unfiltered-html[i].href"];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.wp:action-assign-author"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
href = [jsonResponse StringOf: @"_links.wp:action-assign-author[i].href"];
i = i + 1;
}
i = 0;
count_i = [[jsonResponse SizeOfArray: @"_links.curies"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
name = [jsonResponse StringOf: @"_links.curies[i].name"];
href = [jsonResponse StringOf: @"_links.curies[i].href"];
templated = [jsonResponse BoolOf: @"_links.curies[i].templated"];
i = i + 1;
}