Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

How to insert image using WP REST API? #2563

Closed
ghost opened this issue Jun 27, 2016 · 12 comments
Closed

How to insert image using WP REST API? #2563

ghost opened this issue Jun 27, 2016 · 12 comments
Labels

Comments

@ghost
Copy link

ghost commented Jun 27, 2016

I'm using WP REST API to post article into wordpress. How to insert image using WP REST API in the post?

I tried using<img src="url_to_img.png" /> in the post, now the html code displayed as code but not image in my post.

@azhard4int
Copy link

@dodoflying , can you paste your code over here, it will give us better understanding and help you to solve your problem.

@ghost
Copy link
Author

ghost commented Jun 28, 2016

I tried using<img src="url_to_img.png" > directly in the post, now the html code displayed as code but not image in my post.

 $content='text text <img  src="url_to_img.png"> text';
         $data = array(
                'title' => $title,
                'date' => $post_date,
                'content' => $content,
                'status' => 'publish',
                'categories' => array(4),
                'tags' => array(7,6,5),
                'featured_media' => $images[$rand_key]

            );

        $response = wp_remote_post( $url, array (
            'method'  => 'POST',
            'headers' => $headers,
            'body'    =>  $data
        ) );

@almirbi
Copy link

almirbi commented Jun 30, 2016

Hello there,

the way you tried it is just to add HTML into the content field. It will not magically find and upload the picture you added as source.

What you have to do is first call the media endpoint in order to upload the picture, and then pass the ID of the added media into the featured_media field.

Keep in mind that when doing a POST on /wp/v2/media, you need to specify the filename you want to upload in the header, like this:

$headers['Content-Disposition'] => 'attachment; filename=url_to_img.png'

Cheers

@ghost
Copy link
Author

ghost commented Jul 1, 2016

Thanks Almirbi! I just want to insert a picture from a picture CDN website instead of uploaded to my own website. Any suggestions to do this without upload a picture?

@almirbi
Copy link

almirbi commented Jul 1, 2016

Add a custom field where you will pass in the URL of your picture from a CDN, and then render it from that custom field instead from featured image. Keep in mind you have to make sure that WP-API knows about that custom field.

http://v2.wp-api.org/extending/modifying/

EDIT: even though since you are hosting the image elsewhere, and if you provide the full URL to the post content ( as you initially did ), it should actually work. But nonetheless I would recommend adding a custom field, it's cleaner and easier to update.

Cheers

@rmccue rmccue closed this as completed Jul 3, 2016
@dleeward
Copy link

dleeward commented Jul 5, 2016

You could also use the Jetpack API from Wordpress.com to do what you want to do. See the media_urls field at https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/media/new/ We use it without any problem.

There was an issue here last year that discussed adding this capability, but I can't find it.

@ghost
Copy link
Author

ghost commented Jul 7, 2016

@almirbi , i read the documents but still don't know how to "Add a custom field " for the image. Can you give me a example?

Thanks!

@kadamwhite
Copy link
Contributor

I'm actually a little confused about your desired end goal: can you clarify whether

  • You have an image already available by URL on a CDN, and wish to include that image inline within a WordPress post; or
  • You have an image already available by URL on a CDN, and wish to use that image as a featured image; or
  • You have an image you wish to upload to the CDN?

@ghost
Copy link
Author

ghost commented Jul 25, 2016

Sorry about that. My question is

I already have an image available by URL on a CDN, and wish to include
that image inline within a WordPress post;

Regards,
Don

On Mon, Jul 25, 2016 at 12:00 PM, K.Adam White notifications@github.com
wrote:

I'm actually a little confused about your desired end goal: can you
clarify whether

  • You have an image already available by URL on a CDN, and wish to
    include that image inline within a WordPress post; or
  • You have an image already available by URL on a CDN, and wish to use
    that image as a featured image; or
  • You have an image you wish to upload to the CDN?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#2563 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAe1803Wtvz2Ce6Uqbj7r6zOGAQUG_jyks5qZOu4gaJpZM4I_D4z
.

@kadamwhite
Copy link
Contributor

kadamwhite commented Jul 25, 2016

If that is the case, @dodoflying, I would expect that including the <img> tag in the HTML of your post should be working. There is no added interaction with the /media endpoint necessary if the image is not supposed to live within WordPress.

I made a quick test and POSTing this object:

{
  "title": "has an image",
  "status": "publish",
  "content": "<img src=\"http://path.to.a/image/file.jpg\" />"
}

successfully created a post with the title "has an image" and the image tag properly visible in its content. What method are you using to create your post object?

@ghost
Copy link
Author

ghost commented Jul 28, 2016

Thanks kadamwhite. It's working now. I guess i forgot to escape the double quote.

@kadamwhite
Copy link
Contributor

@dodoflying glad to hear it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants