5

I would like to post on my Wordpress blog using API. Since I'm in a Javascript application I would do that using this language.

I have made some searches and I have found node-wpapi package, that uses Wordpress XML-RPC protocol. Everything works, except posting article with media or featured image.

const responseUploadImage = await wp.media()
    .file('./tempImage.jpg')
    .create({
        title: 'My awesome image',
        alt_text: 'an image of something awesome',
        caption: 'This is the caption text',
        description: 'More explanatory information'
    });

const responsePostCreation = await wp.posts().create({
    title: 'Your Post Title',
    content: 'Your post content',
    // status: 'publish'
});

const response = await wp.media().id(responseUploadImage.id).update({
    post: responsePostCreation.id
});

It does create post and upload media, but it doesn't create post with media.

Do you know alternative or a better way to create posts with media and featured image with a JS library?

smartmouse
  • 13,912
  • 34
  • 100
  • 166
  • 1
    I am not familiar with the node library you are using but I do have a Postman collection here for Wordpress API: https://explore.postman.com/templates/3073/wordpress-api-v2 – Kyle Calica-St Dec 26 '19 at 17:24
  • I don't see where your async function is where you call both functions to create those as well. Is this all the code? – Kyle Calica-St Dec 26 '19 at 17:30
  • 1
    This is a good article on how to do so with the library: http://wp-api.org/node-wpapi/guides/2016/08/15/create-a-post-with-featured-media.html I see you made two requests as you should. One to create to upload the post and a second to create the post. What I am not seeing is a request to associate the two? – Kyle Calica-St Dec 26 '19 at 17:30
  • 1
    since you are creating both the post and the media at the same time, have you tried creating the post first, then creating the media with the `post` field directly (instead of doing a separate update)? I don't know why it would be any different functionally, but it might be worth a try (and it would save a request). – Robb Traister Dec 26 '19 at 18:17
  • @RobbTraister Inverting the order of the calls did not do the trick. – smartmouse Dec 26 '19 at 22:41

1 Answers1

4

To set a featured image when creating a post, just provide a featured_media parameter. Example:

wp.media().file("test.jpg").create({
    title: "Media Title"
}).then(media => {
    return wp.posts().create({
        title: "Your Post Title",
        content: "Fancy",
        featured_media: media.id
    })
}).then(post => {
    // Success
}).catch(() => {
    // Error
})

To insert the image into the post content, you can put an <img> tag in the content parameter. Example:

wp.media().file("test.jpg").create({
    title: "Media Title"
}).then(media => {
    return wp.posts().create({
        title: "Hi",
        content: `<img src="${media.source_url}" />`
    })
})

Both of these have been tested against WordPress 5.3.2. I hope this helps!

Caleb Denio
  • 1,465
  • 8
  • 15
  • I'm getting this error: **Error: .field(name, val) val can not be empty**. It comes from `wp.media().file("test.jpg").create` function. More details here: https://pastebin.com/vyTTs0Q0 – smartmouse Jan 01 '20 at 22:41
  • Hmm... It's working for me, so I'd recommend running `npm update` to make sure you're on the latest version of `superagent` (the module thats throwing the error) – Caleb Denio Jan 02 '20 at 00:48
  • It's possible that you're not passing anything into the `create()` function. That's the best I can do without seeing other code. – Caleb Denio Jan 02 '20 at 01:05
  • The weird thing is that if I run just post creation (using the code taken from here https://github.com/WP-API/node-wpapi#creating-posts) it returns the list of existing posts, instead of create new one. – smartmouse Jan 02 '20 at 08:51
  • 1
    Solved, it was an authentication issue that I fixed adding a line in the `.htaccess` file: https://github.com/WordPress/application-passwords/wiki/Basic-Authorization-Header----Missing – smartmouse Jan 02 '20 at 09:15