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 badges2 Answers
Reset to default 5Like 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).
- Install the Application Passwords plugin
- Maybe create a new user (e.g. username: wpapi)
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
版权声明:本文标题:javascript - How to create a new WordPress post, using the wpapi for Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745213026a2648001.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论