5

I'm using the WP REST API plugin V2 (http://wp-api.org/).

Can the API upload a featured image and generate the related meta data?

I know I can upload an image attachment (POST /wp-json/wp/v2/media) and then update the related article (PUT /wp-json/wp/v2/posts/ID) and make its "featured_image" key point to the attachment id.

But is this the right way to do this?

Is it possible to generate different (resized) versions of the featured image after upload, or does this require a custom endpoint?

user2297996
  • 1,382
  • 3
  • 18
  • 28

4 Answers4

12

I know I can upload an image attachment (POST /wp-json/wp/v2/media) and then update the related article (PUT /wp-json/wp/v2/posts/ID) and make its "featured_image" key point to the attachment id. But is this the right way to do this?

As far as I can tell thats the way to go. WP API docs is "a bit" short on explaining all this. And quite some frustration was involved but in the end thats exactly how I got it working.

So first upload the media to endpoint POST /wp-json/wp/v2/media, with the following HTTP headers and the file contents as data:

            'CURLOPT_HTTPHEADER' => [
            'Content-type: application/json',
            'Authorization: Basic ' . $base64Credentials,
            'Content-Disposition: attachment; filename="acme.png"'
            ]

The catch here was the Content-Disposition header. This call should return the media ID, which you will need now for calling POST /wp-json/wp/v2/posts/{$existingPostId}.

Same headers but without the Content-Disposition. This time the data should be JSON encoded {"featured_media": 156}

(you dont have to use CURL directly. Just be sure to pass in the HTTP headers into the request)

pHiL
  • 1,722
  • 18
  • 19
  • I am following this advice and i obtain the error `Sorry, this file type is not permitted for security reasons`. – Brethlosze Jan 16 '19 at 14:14
  • the filename should have an allowed extension eg `tmp` is rejected with error `not permitted for security reasons` but `tmp.jpg` and `tmp.png` allowed – Dr Deo Feb 02 '19 at 05:44
  • i think content-type should be image/png in this example, and iamge/jpeg for a .jpg image – dbu Jun 16 '19 at 07:37
4

To do this in one step, you can add a filter in PHP like this:

add_filter('rest_prepare_attachment', 'attach_media_to_post',10,3); 
function attach_media_to_post($response, $post, $request) {
    if($request->get_method()!='POST'){
        return $response;
    }       
    $parameters = $request->get_params();       
    if(isset($parameters['featured'])){
        set_post_thumbnail($parameters['featured'],$post->ID);
    }
    return $response;
}

So, your call can pass a parameter for post ID to attach the media. Something like this:

http://yoursite.com/wp-json/wp/v2/media?featured=1234
3

File uploading worked in my side through following method:

Method: POST
URL: https://domainname/wp-json/wp/v2/media
Body: form-data
    Key=file
    Value=attached file

Header:
     Content-Type: application/x-www-form-urlencoded
vahdet
  • 6,357
  • 9
  • 51
  • 106
1

I was able to create a post with an image id from uploading an image (ionic mobile). I found the secret sauce by examining the the json response from wp-json/wp/v2/posts/id

In the data section I saw "featured_media", I had read that I needed to set "featured_image" to an id but that didn't work.

So I tried setting the "featured_media" value in my post and it works.

I tried everything, nothing worked until I tried this.

The "post_meta" array had no effect whatsoever, but I left it in there because that's what they said to do in the (sparse) documentation I could find.

Here's what worked for me:

$scope.http({
             method: "POST",
            url: "http://<domain>/wp-json/wp/v2/posts" ,
            headers: { 'Authorization' : 'Basic ' + $scope.au },
            data: {
            title: $scope.postData.title,
            content: $scope.postData.content,
            status: $scope.postData.status,
            featured_media: $scope.imageid,
            post_meta: [
                {
                    "key": "_thumbnail_id",
                    "value":  $scope.imageid
                }] 
            }
robtown
  • 21
  • 1
  • 1
    I have to say that for me it dows work with setting `featured_media` to a previous uploaded media id. just as its supposed to work. – pHiL Jul 19 '16 at 22:32