WordPressDotNet: A simple WP REST API library for .NET
Last week, I wrote a small portable class library, WordPressDotNet, for retrieving blog posts, comments, authors and media. All you have to do, is to install WP-REST-API v2 plugin on your WordPress deployment for consuming the API. I will walk you through the process of using the library.
*
Before you can use the library, you have to download and install WP-REST-API v2 plugin.
*
Installing the plugin on WordPress
- Download the plugin
- Log in to your WordPress
- Go Plugins > Add New
- Click on Upload Plugin
- Click on Activate Plugin
The NuGet Package
We will use the library in a console application. Create a console application. And follow the following steps.
Installing NuGet Package
Use NuGet Package manager console
PM Install-Package wordpressdotnet
Or go to https://www.nuget.org/packages/wordpressdotnet
Using the library
There is a class
named WordPressConnector
. Make an instance of the class
, with URL of your site as argument.
var connector = new WordPressConnector("[WordPressSiteUrl]");
Getting Posts
To GET posts, use .Posts.GetPostsAsync()
method. For a single post, use .Posts.GetPostAsync([PostId])
method. Example
var posts = await connector.Posts.GetPostsAsync();
For accessing posts, media, comments and authors, use the instance members Posts
, Media
, Comments
and Authors
respectively.
I am giving example of how you can access Posts. Explore the library for Authors, Comments and Media ☺
Paging
WordPressDotNet
gets the results in pages. The following example demonstrates how you can use paging.
By default, the results per page is set to 10.
|
|