Benefits of Coding in C# for WordPress Plugins

Clarity eCommerce - The eCommerce Platform to Scale and Grow Your Business
WordPress Guide: Relationship Between C# And WordPress?

What are WordPress Plugins and Why is C# Important?

WordPress, initially released on May 27, 2003, is an open-source content management system (CMS) written in PHP. Ever since its release, it has received a lot of favors from people across the world. Well, that's mostly because of its plugin oriented design. Currently, there are more than 50,000 plugins available on the WordPress database for its users to use. However, since WordPress is written in PHP, all the available plugins are also written in the same language. But guess what, you can always write WordPress plugins in other programming languages, such as C#. In case you don't know about it before, check below to learn about the benefits of C# and how you can write your first WordPress Plugin in C#.

The Ultimate Guide to WordPress Multisite - Step By Step Procedure
Here's What You Need to Know About Writing A WordPress Plugin In C#

Getting Started with a C# Plugin

There are a few PHP compilers out there for .Net that you can employ to write a WordPress plugin in C#. One of the most popular ones is called Phalanger. Phalanger project has made it in such a way that people can run virtually unmodified WordPress clones on .Net. However, there are a few challenges that the project couldn't address, and one of them is the issue of compatibility due to the project having not been compatible with new releases from WordPress. Fortunately, there's a successor of Phalanger, called Peachpie, which has now addressed the issue.

Peachpie, initially released in mid-2016, is an opensource PHP compiler and runtime for .NET and .Net core frameworks. With it, you can be able to write a WordPress plugin in C# efficiently. According to the Czech developer, there are lots of improvements on the Peachpie compiler, which is based on the first-generation Phalanger project. One of them is that Peachpie has an open-source project, called ImageSharp, which helps the compiler to perform similar image function as in PHP. That said, let's take a look at how WordPress plugins in C# work below.

Starting with a dummy plugin for WordPress, plugin.php. For that, we'll use the following code to the right:

<?PHP
/*
Plugin Name: dotNET WordPress Plugin
Description: Basic Plugin in .NET
Version: 20171019
Author: peachpie.io
Author URI: https://www.peachpie.io/
License: Apache
*/

\MyWpPlugin::Register();

Setting up Your WordPress Multisite

Next, we need to implement "MyWpPlugin::Register()" and make it visible to the compiled PHP code. We'll be able to do that by proceeding to create a C# project, "plugin.csproj". Here's how that looks like below.

use Pchp.Core; // Peachpie core API
public class MyWpPlugin {
public static void Register(Context ctx) {
// register callbacks into ctx
}
}

Next, we need a reference to the Peachpie Runtime library('Peachpie.Runtime'). Speaking of that, it contains the API that works with the compiled PHP program. In the code above, the 'Context ctx' parameter is there to help "manipulate the PHP runtime". Apart from that, you can also use it to simulate PHP invokes. Check below for the reference XML snippet, which you can add to the "msbuildproj" file. <ProjectReference Include="..\plugin\plugin.csproj" />

With that, you can now rest assured that everything you implement in your C# code will be available to your PHP code.

The Final Steps to Getting Started

Write Your First WordPress Plugin In C#

Now, it's time to write your first WordPress plugin in C#. You can do that by using the short code below:

use System;
use Pchp.Core; // Peachpie core API
public class MyWpPlugin {
public static void Register(Context ctx) {
ctx.Call("add_filter", "the_content", new Func<string, string>(text =>
{
text = text.Replace("Welcome", "Greetings");
return text;
}));
}
}

One of the purposes of the code above is to help in adding filters to WordPress. Speaking of the function, it has two parameters, including the filter name and a callable. As far as the callable is concerned, it's called whenever the filter has to be invoked, by WordPress. Furthermore, there's a C# delegate in place of the callable. It prevents WordPress from noticing that the code is not a PHP callable. You can have a look at it below:

// THIS ACTUALLY CALLS OUR C# DELEGATE!!
$value = call_user_func_array( $the_['function'], $args );

You can check GitHub for more information regarding writing WordPress plugin in C#.

Related Posts