Easily Turn a WordPress Site Into a Desktop App

Easily Turn a WordPress Site Into a Desktop App

Everyone is eager to turn their WordPress site into a mobile app, and we offer two services, Reactor and AppPresser, to help you to do that. However, as an alternative (or in tandem!), a desktop app may provide a good user experience, so I’m here to walk you through turning your WordPress site into a desktop app.

We will be using Electron, an open source framework for turning web technologies into desktop apps for Mac, Windows and Linux. You may have heard of many desktop apps powered by Electron, including Atom EditorSlack, Microsoft’s Visual Studio and the recently debuted WordPress.com app.

Electron is built and run with Node so you want to make sure you have Node installed. Electron is installed as a Node module. Once you have Node installed, fire up the command line!

Install dependencies

First, we need to install Electron and the packager modules globally.

$ npm install electron-prebuilt -g

$ npm install electron-packager -g

Create App Folder

Then we need to create a folder for the app…

$ mkdir app

$ cd app

Configure App

Next, we will run npm init to create the package.json file. We put configuration data for Electron in this file.

$ npm init

There will be a few configuration steps to go through; you can enter them in the command line or edit package.json directly. I prefer to skip and enter the file directly.

The package.json file will be located in the app folder. Open this file and you should see something like so:

  1. {
  2. name“: testing“,
  3. version“: 1.0.0“,
  4. description“: “”,
  5. main“: index.js“,
  6. scripts“: {
  7. test“: echo \”Error: no test specified\” && exit 1
  8. },
  9. author“: “”,
  10. license“: ISC
  11. }

We need to enter a few things in the configuration file. Under scripts, add a start script and enter "electron ." as the value. This will run Electron from the command line. Also, add dependencies for electron-packagerand electron-prebuilt.

  1. {
  2. name“: WebDevStudios“,
  3. version“: 1.0.0“,
  4. description“: “”,
  5. main“: index.js“,
  6. scripts“: {
  7. start“: electron .”,
  8. },
  9. author“: “”,
  10. license“: MIT“,
  11. devDependencies“: {
  12. electronpackager“: “”,
  13. electronprebuilt“: “”
  14. }
  15.  
  16. }

App JavaScript File

If we tried to start the app now it’d give you an error saying it’s not an Electron app. Create an index.js in the app folder Electron will load this file when the app runs. This file is called in the package.json file under “main”. This file will contain the main JavaScript for Electron. It tells Electron what to do and how to display your app.

The first thing to do is to require Electron.
const electron = require('electron');

This const is to control app life
const app = electron.app;

Electron runs in a Chrome browser via Chromium. We create a const to access the browser to tell it what to display and to bridge the JavaScript with native OS code.
const BrowserWindow = electron.BrowserWindow;

Read through below to learn the required code; it should be self-explanatory.

  1. use strict‘;
  2.  
  3. const electron = require(‘electron‘);
  4. const app = electron.app; // Module to control application life.
  5. const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
  6.  
  7. // Keep a global reference of the window object, if you don’t, the window will
  8. // be closed automatically when the JavaScript object is garbage collected.
  9. var mainWindow = null;
  10. var url = http://webdevstudios.com/blog’;
  11.  
  12. // Quit when all windows are closed.
  13. app.on(‘windowallclosed‘, function(event) {
  14. // On OS X it is common for applications and their menu bar
  15. // to stay active until the user quits explicitly with Cmd + Q
  16. if (process.platform != darwin‘) {
  17. app.quit();
  18. }
  19. });
  20.  
  21. // This method will be called when Electron has finished
  22. // initialization and is ready to create browser windows.
  23. app.on(‘ready‘, function() {
  24. // Create the browser window.
  25. mainWindow = create_window();
  26. load_window();
  27.  
  28. // Emitted when the window is closed.
  29. mainWindow.on(‘closed‘, function(event) {
  30. mainWindow = null;
  31. });
  32.  
  33. });
  34.  
  35. // when you click the app dock icon to re-initilize window
  36. app.on(‘activate‘, function() {
  37. if ( mainWindow == null ) {
  38. mainWindow = create_window();
  39. load_window();
  40. // Emitted when the window is closed.
  41. mainWindow.on(‘closed‘, function(event) {
  42. mainWindow = null;
  43. });
  44.  
  45. }
  46. });
  47.  
  48. // create a window function
  49. function create_window() {
  50. return new BrowserWindow({
  51. width: 800,
  52. height: 600
  53. });
  54. }
  55.  
  56. // load window function, loads the url into browser
  57. function load_window() {
  58. mainWindow.loadURL(url);
  59. }

Run App

Now that you have the index.js file filled in with code Electron needs to initialize you can run the following command. This will run your app. This all you need for Electron app development. In the next section, you’ll learn how to build the app project into a distributable app.

$ npm run start

Build App

To build the app we need to add a script to the package.json file for the electron-packager module. This build script will download the files to build an app for the platform you want to create a distributable app for. In our example we are building for OSX. Read more about the packager and how to build for other platforms.

  1. {
  2. name“: WebDevStudios“,
  3. version“: 1.0.0“,
  4. description“: “”,
  5. main“: index.js“,
  6. scripts“: {
  7. start“: electron .”,
  8. build“: electronpackager ./ WebDevStudios platform=darwin arch=x64 version=0.36.7 ignore=node_modules/electron-*”
  9. },
  10. author“: “”,
  11. license“: MIT“,
  12. devDependencies“: {
  13. electronpackager“: “”,
  14. electronprebuilt“: “”
  15. }
  16.  
  17. }

After the build script is added run the following command and it will build the app and place it in the project folder. It’s a fully compiled app that you can share.

$ npm run build

Hello awesome!

Turning your WordPress site into a desktop app is not too difficult. There is even an easier way to do this but I think you should go through this tutorial and learn how Electron works then you can add native app features. You could even create a single page app that utilizes the WordPress Rest API. Read through the Electron documentation to add more options to your app. Comment below if you have any issues or questions!

Click here to download an example OS X app that displays the WebDevStudios blog.

*originally published on WebDevStudios

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami