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.
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:
To get started, ensure you have the following installed:
Navigate to your WordPress installation directory and go to:
wp-content/plugins/
wp-content/plugins/my-node-plugin/
<?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. }
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"); });
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.
node server.js
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" }); } });
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.
For more WordPress development tips, check out:
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.