Step by Step: Connect to WordPress Using C#

In this post, we will see how to connect to a WordPress website using C# and make publications using the WordPressPCL library in three easy steps.
WordPress is a great CMS that allows developers and people who don't need a lot of software development skills to create websites with awesome functionalities. Therefore, it is one of the most popular tools used today to build websites.
This CMS is very flexible for creating custom functionalities, although it is very possible that a plugin already exists that can be installed on the site. WordPress has a big community of developers with a lot of experience with the platform where you can find solutions for almost all the issues you can have.
The thing with WordPress is that it was built in PHP, so if you don't know the language you can lose time in the process of learning and then implementing what you want to do.
Next, we will see how to configure a Visual Studio project with C# to connect to a WordPress website, which is a good solution for .NET developers who don't have strong knowledge of PHP.
1. Configure WordPress API in Your Website (Required for Versions Below 4.7)
Note: The plugin WP REST API that enables the REST API was merged into WordPress since version 4.7, so if you have an equal or greater version, just jump to step 2.
To configure your site to enable the API that allows reading and writing on WordPress, the prerequisite to connect to WordPress with WordPressPCL is to install the following plugins on your website:
Then to complete the JWT configuration it is necessary to add some modifications to the .htaccess file:
- First, enable HTTP Authorization Header by adding the following:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
- Then enable WPENGINE by adding this code in the same .htaccess file:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
JWT needs a secret key to sign the token. This secret key must be unique and never revealed.
To add the secret key, edit your wp-config.php file and add a new constant called JWT_AUTH_SECRET_KEY.
define('JWT_AUTH_SECRET_KEY', 'your-top-secret-key');
You can generate and use a string from here: https://api.wordpress.org/secret-key/1.1/salt/
You can see all the details from the documentation here.
2. Install WordPressPCL from NuGet Packages
Now you need to install the WordPress NuGet package from Visual Studio called WordPressPCL. To open NuGet Package Manager, right-click on your project in Visual Studio and then click on Manage NuGet Packages.
Then search WordPressPCL and click Install.
You can also install the package using the NuGet Package Manager Console with the following command:
Install-Package WordPressPCL -Version 1.5.0
The following table shows the supported methods available in WordPressPCL:
Now that we have configured and installed everything you need, let's see how we can create, update, and query data from WordPress.
Example 1: Connecting to WordPress
To connect to a WordPress client you can use the class WordPressClient, which accepts the URL of your website in its constructor. Example: http://domain-example.com/wp-json/. The /wp-json/ is the default path to the WordPress REST API.
private static async Task<WordPressClient> GetClient()
{
// JWT authentication
var client = new WordPressClient("http://wordpress-domain.com/wp-json/");
client.AuthMethod = AuthMethod.JWT;
await client.RequestJWToken("user", "password");
return client;
}
Example 2: Creating and Updating Data
using System;
using System.Linq;
using System.Threading.Tasks;
using WordPressPCL;
using WordPressPCL.Models;
namespace WordPressTest
{
class Program
{
static void Main(string[] args)
{
CreatePost().Wait();
}
private static async Task CreatePost()
{
try
{
WordPressClient client = await GetClient();
if (await client.IsValidJWToken())
{
var post = new Post
{
Title = new Title("New post test"),
Content = new Content("Content for new post.")
};
await client.Posts.Create(post);
}
}
catch (Exception e)
{
Console.WriteLine("Error:" + e.Message);
}
}
private static async Task<WordPressClient> GetClient()
{
// JWT authentication
var client = new WordPressClient("http://wordpress-domain.com/wp-json/");
client.AuthMethod = AuthMethod.JWT;
await client.RequestJWToken("user", "password");
return client;
}
}
}
Example 3: Getting Data
Querying data is straightforward, as you can see below:
var client = GetClient();
// Getting all posts
var posts = await client.Posts.GetAll();
// Getting one post
var onePost = await client.Posts.GetByID(1);
You can download these examples from my GitHub account in the project WordPressTest. For more information about the WordPressPCL API, check the official documentation here: GitHub WordPressPCL Documentation.
I hope this gives you a good introduction to connecting with a WordPress website using .NET.
Please let me know if you have any questions.


