最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a new WordPress post, using the wpapi for Node.js? - Stack Overflow

programmeradmin0浏览0评论

I tried using the wpapi module to create a post in WordPress. There is no error, the request ends with 200 Success, but the request body is an empty object and there is no post created.

var wp = new WPAPI({
   endpoint: '',
   username: 'someusername',
   password: 'password'
});
wp.posts().create({
   title: 'Your Post Title',
   content: 'Your post content',
   status: 'publish'
}).then(function( response ) {
   console.log( response.id ); // This is undefined
})

Why is that and how can I fix that?

I tried using the wpapi module to create a post in WordPress. There is no error, the request ends with 200 Success, but the request body is an empty object and there is no post created.

var wp = new WPAPI({
   endpoint: 'http://your-site./wp-json',
   username: 'someusername',
   password: 'password'
});
wp.posts().create({
   title: 'Your Post Title',
   content: 'Your post content',
   status: 'publish'
}).then(function( response ) {
   console.log( response.id ); // This is undefined
})

Why is that and how can I fix that?

Share Improve this question edited Dec 9, 2019 at 10:00 Ionică Bizău 114k94 gold badges310 silver badges487 bronze badges asked Dec 9, 2019 at 9:59 GhitaBGhitaB 3,4375 gold badges36 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Like I mentioned here, I fixed this by using the Application Passwords plugin.

The problem can appear if you have Duo Authentication and the WP API rest client fails to parse the JSON response (which is invalid indeed), but it fails silently (they have a try-catch there).

  1. Install the Application Passwords plugin
  2. Maybe create a new user (e.g. username: wpapi)
  3. Create an application password for that user and then use it in the Node.js code:

    var wp = new WPAPI({ 
      endpoint: 'http://your-site./wp-json',
      username: 'wpapi',
      password: 'XXXX XXXX XXXX XXXX'
    });
    

That will bypass the Duo Authentication.

You can fix this with the [Application password][1] plugin! It's for authenticating API requests such as REST API and XML-RPC. It will bypass the two-factor authentication and helps to authenticate users without providing their passwords directly. Instead, a unique password is generated for each application without revealing the user’s main password.

Use this code only!

var wp = new WPAPI({
   endpoint: 'http://your-site./wp-json',
   username: 'someusername',
   password: 'password'
});
发布评论

评论列表(0)

  1. 暂无评论