WordPress and NODE

When Calypso came out, many were afraid that WordPress 4 would be purely in Javascript.  I wouldn't worry.  Websites that require large data will always need PHP for speed and scalability.  I've watched young node js developers swear off PHP, and build apps entirely with node that aren't very responsive or fast.  Two reasons we use javascript today.

And the main reason for this is because you just can't have a site that handles a ton of data, and expect it to function well by building it entirely with Javascript.

Which is why I love combining these two technologies.  I'm currently working on a CMS system called PAM, which is PHP, Angular and Mongodb.  But this is still a work in progress and I'm not ready to release this code just yet. However, if you want to be a contributor to PAM, please do contact me.

In the meantime, I love using newer technologies that node provides, in my WordPress sites that I build for clients.

Here's a quick primer for anyone wanting to combine WordPress or even Drupal with node js.


I'm assuming that you're on a MAC OSX system with Homebrew installed. The line below will install Homebrew for Mac in case you don't have it.

Install Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)";

Install NODE

brew install node;

Check your node version and make sure you have the latest version of NPM, the node.js package manager.

node -v;
npm install npm@latest -g;

Finally, we want to make sure that brew is updated and upgraded since node.js is updated frequently.

brew update; brew upgrade;

Using Node with WordPress

Now that node is installed, we are ready to work add node.js to our wordpress installation. Many people to choose to install node modules in their theme directories, but I prefer to install node on a global level in the root WordPress directory so that all themes can use installed node modules.

Our first step should be to initialize our node project and creating a package.json file.

npm init;

Answer the questions at the prompt to automatically create a package.json file.

Install Gulp

Our next step is to install a task runner, and gulp is much better than grunt if you ask me. The first line below with the "g" flag installs gulp globally, so that you can use it while in any directory.

The second line will add gulp to your package.json file. Which means that if a coworker pulls down your code from GIT, all the have to do is type in "npm install" to install every node module in the package.json file

This is why you should have a .gitignore file if you're using GIT, and that the node_modules folder is in this .gitignore file. You don't want these node modules to go into GIT, which is redundant.

npm install --global gulp-cli; npm install --save-dev gulp; Your next step is to create a gulpfile, which is as simple as creating a file named "gulpfile.js" in the root directory.

For now, just put the following code into your gulp file, and save it. Then simply type "gulp" into your terminal.

You're Done!

That's basically it! You have everything you need to use WordPress with node.js.

Make sure to check out my post about a example gulp file.