WordPressDotNet: A simple WordPress library for .NET

Want create site? Find Free WordPress Themes and plugins.

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

  1. Download the plugin
  2. Log in to your WordPress
  3. Go Plugins > Add New
  4. Click on Upload Plugin
  5. 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.

static void Main(string[] args)
        {
            var connector = new WordPressDotNet.WordPressConnector("http://localhost/wordpress/");

            // Page 1 (with 2 results per page)
            Console.WriteLine("Page 1 results:\n---------------");

            var posts = connector.Posts.GetPostsAsync(1,2);
            posts.Wait();
            foreach(var post in posts.Result)
            {
                Console.WriteLine(post.title.rendered+"\n");
            }
            // Page 2 (with 2 results per page)
            Console.WriteLine("Page 2 results:\n---------------");
            posts = connector.Posts.GetPostsAsync(2,2);
            posts.Wait();
            foreach (var post in posts.Result)
            {
                Console.WriteLine(post.title.rendered + "\n");
            }


        }
Did you find apk for android? You can find new Free Android Games and apps.
Malak Zia Nasir

Author Malak Zia Nasir

Software Engineer, a Microsoft guy. Working on multiple platforms. A lover, a miskeen :P

More posts by Malak Zia Nasir

Leave a Reply