Blue background with white text that has title of post "Creating Installable Zip Files Of WordPress Plugins With Plugin Machine"

Creating Installable Zip Files Of WordPress Plugins With Plugin Machine

Plugin Machine helps WordPress plugins with both development and delivery of WordPress plugins. The Plugin Machine CLI can be used for creating automated, predictable “production” ZIP files that are ready to be installed on a WordPress site.

In the past, when I’ve been tasked with creating production ready ZIP files for WordPress plugins it’s often been high stress. I needed to make sure the right files were included, but none of the wrong files. Fun fact: you should never ship phpunit to a server, it’s not designed for that and can create security vulnerabilities.

So instead of relying on me, a human, I like to automate this part. I’ve been trying to find a good solution for awhile. That work eventually evolved into the Plugin Machine CLI’s zip and build commands. I released version 0.8.0 of the CLI today, which adds new build and zip features, which I am going to cover in this post.

When using Plugin Machine as a plugin generator, you are likely to have a lot of files in your plugin related to development that are important for development and testing. That’s great for development, but you do not want to ship those files WordPress.org, your customers or clients.

If you are using npm and/ or Composer to manage dependencies in your plugins, its important that you run the right commands, the same way every time, before releasing your plugins. That’s time consuming and error prone. Instead, we can use the Plugin Machine CLI to automated running those commands and making the ZIP files.

About Plugin Machine CLI

The Plugin Machine CLI is a Node package that you can install from npm. You should install the CLI globally. Full installation and usage documentation can be found here:

https://pluginmachine.com/blog/doc/plugin-machine-cli/

The Plugin Machine CLI is designed to help adopt Plugin Machine for pre-existing plugins. It also helps add features and build release files for plugins, whether they were generated with Plugin Machine or not.

Docker-backed Builds For WordPress Plugins

Ever had problems installing or building a WordPress plugin beacuse of PHP version or node version incompatibilities? I have, it’s such a time waste. When you use Plugin Machine to run the build steps for your plugin, they run in Docker.

This way, all you need to do is download a Docker installer for your OS and click buttons for awhile and your computer will be able to use whichever version of PHP and Node that your plugin requires.

The pluginMachine.json File

Here is an example pluginMachine.json file that was included in a plugin I recently created with Plugin Machine:

{
    "slug": "foxes",
    "pluginId": 177,
    "buildId": 196,
    "entryPoints": {
        "adminPages": [
            "fox-settings"

        ],
        "blocks": []
    },
    "buildIncludes": [
        "foxes.php",
        "readme.txt",
        "php",
        "vendor",
        "build",
        "inc",
        "admin/fox-settings/init.php",
    ],
    "buildSteps": {
        "dev": [
            "npm install",
            "npm start"
        ],
        "prod": [
            "npm install",
            "npm install --production"
        ]
    }
}

Which Files Are Included In Build

The “buildIncludes” key is an array of files or directories that will be included in production builds of the plugin. All directories are added recursively.

You can add files or directories to this array to override the default configuration.

Which Commands Are Run During Plugin Build

The “buildSteps” key has two arrays of build steps to run. The “prod” key is used by the “plugin-machine build” and “plugin-machine zip” commands. Currently the “dev” key isn’t used.

You can add any npm, yarn, composer, or WP-CLI commands to this array to override default configuration.

Building WordPress Plugins For Release

That’s a bit of background, honestly this article could just be this section, but that would be too short.

To run the production build commands and then create a ZIP of the plugin, including only the required files:

plugin-machine plugin build
plugin-machine plugin zip

Runs the same build commands, copy the necessary files to a separate directory and then zip that directory:

plugin-machine plugin build --buildDir=space-people
plugin-machine plugin zip --buildDir=space-people

Don’t Loose Time To The Simple Things

Look, I like playing with all of this stuff. Building this CLI is super fun for me. Most people would rather just focus on their code and getting their work done. Automation is a great solution, if you have time to maintain it. I hope you will give the Plugin Machine CLI a try, the next time building and releasing ZIP files of WordPress plugins is causing problems for you.

I am still working on deployment with Github actions and the Plugin Machine plugin updater API. I’ll write about that when the next major update to the CLI is ready, but you can follow a long here for more updates:

New eBook

Refactoring WordPress Plugins

The PHP Parts

 For experienced WordPress developers who are looking to improve the PHP in their plugins, use new modern best practices and adopt test-driven development.