Aj Khandal

How to Create a WordPress Plugin Using Node.js?

Introduction

WordPress is one of the most popular content management systems, powering millions of websites worldwide. While PHP is its primary language, you can still use JavaScript and Node.js to build plugins. In this guide, we’ll walk you through the step-by-step process of creating a WordPress plugin using Node.js.

WordPress plugin development using Node.js

Why Use Node.js for WordPress Plugin Development?

Node.js is known for its efficiency, scalability, and asynchronous capabilities. While WordPress relies on PHP, integrating Node.js allows you to create high-performance plugins that enhance website functionality.

Key benefits of using Node.js for WordPress plugins:

Step-by-Step Guide to Creating a WordPress Plugin Using Node.js

Step 1: Set Up Your Development Environment

To get started, ensure you have the following installed:

Step 2: Create a Basic WordPress Plugin Structure

Navigate to your WordPress installation directory and go to:

wp-content/plugins/
Create a new folder for your plugin. For example:
wp-content/plugins/my-node-plugin/
Inside this folder, create a main PHP file:
<?php
/**
 * Plugin Name: My Node.js Plugin
 * Description: A WordPress plugin using Node.js.
 * Version: 1.0
 * Author: Your Name
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

Step 3: Set Up Node.js Server

In your plugin folder, initialize a Node.js project:

cd wp-content/plugins/my-node-plugin/
npm init -y

Install Express.js for handling API requests:

npm install express cors body-parser

Create a server.js file inside your plugin folder and add the following code:

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");

const app = express();
app.use(cors());
app.use(bodyParser.json());

app.get("/api/message", (req, res) => {
  res.json({ message: "Hello from Node.js in WordPress Plugin!" });
});

app.listen(3000, () => {
  console.log("Node.js server running on port 3000");
});

Step 4: Connect Node.js with WordPress

Modify the PHP file to call the Node.js API:

function fetch_node_message() {
    $response = wp_remote_get("http://localhost:3000/api/message");

    if (is_wp_error($response)) {
        return "Error fetching data";
    }

    $body = wp_remote_retrieve_body($response);
    return json_decode($body)->message;
}

add_shortcode("node_message", "fetch_node_message");

Now, you can use [node_message] shortcode in WordPress to display the message from the Node.js server.

Step 5: Test Your Plugin

node server.js

Example Use Case: Fetching Live Cryptocurrency Prices

Let’s say you want to display live cryptocurrency prices on your WordPress site using a Node.js API. You can modify your Node.js server like this:

const axios = require("axios");

app.get("/api/crypto", async (req, res) => {
  try {
    const response = await axios.get("https://api.coindesk.com/v1/bpi/currentprice.json");
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "Error fetching data" });
  }
});

Then, update your WordPress PHP file to fetch the data:

function fetch_crypto_price() {
    $response = wp_remote_get("http://localhost:3000/api/crypto");

    if (is_wp_error($response)) {
        return "Error fetching data";
    }

    $body = wp_remote_retrieve_body($response);
    return json_decode($body)->bpi->USD->rate;
}

add_shortcode("crypto_price", "fetch_crypto_price");

Now, using [crypto_price] in a WordPress post will display the current Bitcoin price.

Best Practices for WordPress Plugin Development Using Node.js

Interlinking & Further Reading

For more WordPress development tips, check out:

Conclusion

Creating a WordPress plugin using Node.js opens up new possibilities for developers who prefer JavaScript over PHP. By leveraging the power of Node.js, you can build high-performance plugins that enhance WordPress functionality.

Do You Have An Interesting Project? ​

$10/ HR
Need Help?