admin管理员组

文章数量:1415119

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'
});

本文标签: javascriptHow to create a new WordPress postusing the wpapi for NodejsStack Overflow