Resource Information
Method | POST |
---|---|
URL | https://public-api.wordpress.com/rest/v1.1/sites/$site/media/new |
Requires authentication? | Yes |
Method Parameters
Parameter | Type | Description |
---|---|---|
$site | (int|string) | Site ID or domain |
Query Parameters
Parameter | Type | Description |
---|---|---|
context | (string) |
|
http_envelope | (bool) |
|
pretty | (bool) |
|
meta | (string) | Optional. Loads data from the endpoints found in the 'meta' part of the response. Comma-separated list. Example: meta=site,likes |
fields | (string) | Optional. Returns specified fields only. Comma-separated list. Example: fields=ID,title |
callback | (string) | An optional JSONP callback function. |
Request Parameters
Parameter | Type | Description |
---|---|---|
media | (media) |
An array of media to attach to the post. To upload media, the entire request should be multipart/form-data encoded. Accepts jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. Audio and Video may also be available. See allowed_file_types in the options response of the site endpoint.Example: curl \ |
media_urls | (array) | An array of URLs to upload to the post. Errors produced by media uploads, if any, will be in `media_errors` in the response. |
attrs | (array) |
An array of attributes (`title`, `description`, `caption` `alt` for images, `artist` for audio, `album` for audio, and `parent_id`) are supported to assign to the media uploaded via the `media` or `media_urls` properties. You must use a numeric index for the keys of `attrs` which follows the same sequence as `media` and `media_urls`. Example: curl \ |
Response Parameters
Parameter | Type | Description |
---|---|---|
media | (array) | Array of uploaded media objects |
errors | (array) | Array of error messages of uploading media failures |
Resource Errors
These are the possible errors returned by this endpoint.
HTTP Code | Error Identifier | Error Message |
---|---|---|
403 | unauthorized | User cannot access this private blog. |
403 | unauthorized | User cannot access this restricted blog |
403 | unauthorized | User cannot upload media. |
404 | unknown_media | Unknown Media |
Example
curl \ -H 'authorization: Bearer YOUR_API_TOKEN' \ --data-urlencode 'media_urls=https://s.w.org/about/images/logos/codeispoetry-rgb.png' \ 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/new'
<?php $options = array ( 'http' => array ( 'ignore_errors' => true, 'method' => 'POST', 'header' => array ( 0 => 'authorization: Bearer YOUR_API_TOKEN', 1 => 'Content-Type: application/x-www-form-urlencoded', ), 'content' => http_build_query( array ( 'media_urls' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png', )), ), ); $context = stream_context_create( $options ); $response = file_get_contents( 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/new', false, $context ); $response = json_decode( $response ); ?>
Response
{ "media": [ { "ID": 11903, "URL": "https:\/\/apiexamples.files.wordpress.com\/2016\/11\/codeispoetry-rgb11.png", "guid": "http:\/\/apiexamples.files.wordpress.com\/2016\/11\/codeispoetry-rgb11.png", "date": "2016-11-17T06:02:13+00:00", "post_ID": 0, "author_ID": 78972699, "file": "codeispoetry-rgb11.png", "mime_type": "image\/png", "extension": "png", "title": "codeispoetry-rgb11", "caption": "", "description": "", "alt": "", "icon": "https:\/\/s1.wp.com\/wp-includes\/images\/media\/default.png", "thumbnails": { "thumbnail": "https:\/\/apiexamples.files.wordpress.com\/2016\/11\/codeispoetry-rgb11.png?w=150", "medium": "https:\/\/apiexamples.files.wordpress.com\/2016\/11\/codeispoetry-rgb11.png?w=300", "large": "https:\/\/apiexamples.files.wordpress.com\/2016\/11\/codeispoetry-rgb11.png?w=500" }, "height": 34, "width": 500, "exif": { "aperture": "0", "credit": "", "camera": "", "caption": "", "created_timestamp": "0", "copyright": "", "focal_length": "0", "iso": "0", "shutter_speed": "0", "title": "", "orientation": "0", "keywords": [] }, "meta": { "links": { "self": "https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/82974409\/media\/11903", "help": "https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/82974409\/media\/11903\/help", "site": "https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/82974409" } } } ] }