Open kadamwhite opened 7 years ago
FWIW - first time wpapi user here and google brought me here. It would be helpful to include a working snippet. It's obvious now, but at first I didn't realize what I needed to do.
Example of how to list posts at wordpress.com blog using basic auth:
const WPAPI = require('wpapi');
const wpcom = new WPAPI({
endpoint: 'https://public-api.wordpress.com', // not your blog domain
username: 'your-blog-username', // for mywordpresscomblog.wordpress.com
password: 'your-user-password' // for mywordpresscomblog.wordpress.com
});
wpcom.posts().namespace('wp/v2/sites/mywordpresscomblog.wordpress.com')
.get()
.then(console.log)
.catch(console.error);
This issue was raised in Gitter chat:
The built-in WordPress REST API is supported by WordPress.com through their public-api.wordpress.com site; the base URL for endpoints for a given site is e.g.
where
your_site
is a string like "kadamwhite.wordpress.com". Then you can request collections and resources as normal:Because of the structure of this URL, we have to do a bit of adjustment to get our client to generate the proper URL format. The simplest way is to chain a
.namespace()
call onto our requests:this is a bit verbose, so you could make a helper method to reduce the duplication:
Alternatively, you should be able to download the API root for your site and use that to bootstrap a client specific to your own .com site.
Note that authentication with the WordPress.com-hosted version of the WordPress REST API is untested, and probably not feasible at this time, since WordPress.com sites provide no plugins for authentication and arbitrary custom plugins cannot run on WordPress.com.
This issue tracks moving this documentation into the README.