-
Notifications
You must be signed in to change notification settings - Fork 657
How to insert image using WP REST API? #2563
Comments
@dodoflying , can you paste your code over here, it will give us better understanding and help you to solve your problem. |
I tried using
|
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 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 |
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? |
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 |
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. |
@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! |
I'm actually a little confused about your desired end goal: can you clarify whether
|
Sorry about that. My question is I already have an image available by URL on a CDN, and wish to include Regards, On Mon, Jul 25, 2016 at 12:00 PM, K.Adam White notifications@github.com
|
If that is the case, @dodoflying, I would expect that including the 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? |
Thanks kadamwhite. It's working now. I guess i forgot to escape the double quote. |
@dodoflying glad to hear it! |
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.The text was updated successfully, but these errors were encountered: