WP-CLI is a developer-friendly way to manage WordPress sites. WP2Static integrates with WP-CLI, adding useful commands to generate and deploy your static site.

WP-CLI commands

Running the command wp wp2static will give you all the avaiable options and is the best way to know which commands are available on your system. WP2Static Add-ons may provide additional commands.

WP-CLI commands will apply to the site relative to the system path you run them from.

System user when running WP-CLI

TL;DR - run wp commands as the same user as your webserver

When WP-CLI runs, it will likely be using the system default PHP, which executes in a different environment than when accessing your WP site via the webserver. This means that the PHP configuration is likely pulled from a php.ini vs a php-fpm.conf type file, so check you have any webserver environment customizations you need also in your CLI PHP environment.

It also means that when you run the wp command from the CLI, you’ll be doing so as the currently logged in user, unless specified otherwise. For WP2Static and other plugins which touch files on the webserver, this can lead to issues with file permissions, ie - mylocaluser runs wp wp2static generate, which sets files with ownership of mylocaluser, but then when you perform an action via the webserver, your webserver user (www, www-data, etc) may not have permissions to use those files, resulting in unexpected errors.

To change the user who runs wp commands:

OpenBSD

Edit your /etc/doas.conf file, adding a line:

permit persist MYUSERNAME as www cmd wp

You may then choose to alias the command for easier usage. In your ~/.profile:

alias wp='doas -u www wp'

Linux/macOS/Windows

Try something similar with sudo -u www wp or runas /noprofile /user:mymachine\www wp. Let us know what works and we’ll update this doc.

Multisite WP2Static WP-CLI commands

When running WordPress in a Network/Multisite setup, add the --url=<url> argument to force the command to be applied to the WordPress site with that Site URL.

WP-CLI examples


Install and activate WP2Static, set options and generate static site Zip archive.

  • wp plugin install --activate static-html-output-plugin
  • wp wp2static options set selected_deployment_option zip
  • wp wp2static options set baseUrl https://mystaticsite.netlify.com
  • wp wp2static generate
  • wp wp2static deploy

A new ZIP archive will be created and path can be retrieved with:

echo "$(cat wp-content/uploads/WP2STATIC-CURRENT-ARCHIVE.txt | sed 's/.$//')".zip

/wpinstallpath/wp-content/uploads/wp-static-html-output-1574438458.zip

Note: the plugin’s slug, as used within the official wp.org repo is static-html-output-plugin, not wp2static. Because, reasons.


Generate static site on server and use Netlify’s CLI tool to deploy

This is a good option for very large sites deploying to Netlify, as only changes will be deployed each time.

  • wp wp2static options set selected_deployment_option folder
  • wp wp2static options set baseUrl https://mystaticsite.netlify.com
  • wp wp2static generate
  • cd "$(cat wp-content/uploads/WP2STATIC-CURRENT-ARCHIVE.txt)"
  • netlify deploy

The Netlify CLI will give you an interactive prompt to follow.


Generate static site on server and deploy via sFTP

Until there is a dedicated Add-on supporting sFTP, the following commands will work.

  • wp wp2static options set selected_deployment_option folder
  • wp wp2static options set baseUrl https://myftpserver.com
  • wp wp2static generate
  • EXPORT_DIR="$(cat wp-content/uploads/WP2STATIC-CURRENT-ARCHIVE.txt)"
  • scp -rp "$EXPORT_DIR" user@server:dest

Generate static site on server and deploy to CloudFlare Workers via Wrangler

Until there is a dedicated Add-on supporting CloudFlare workers, the following commands will work.

  • wp wp2static options set selected_deployment_option folder
  • wp wp2static options set baseUrl https://wp2staticv6test.wp2static.workers.dev/
  • wp wp2static generate
  • EXPORT_DIR="$(cat wp-content/uploads/WP2STATIC-CURRENT-ARCHIVE.txt)"
  • cp -r "$EXPORT_DIR"/* "$MY_WORKER_DIR"/public/
  • cd "$MY_WORKER_DIR"
  • wrangler publish

Above example expects you’ve installed CloudFlare’s Wrangler tool and run wrangler config and wrangler generate --site SITE_NAME. MY_WORKER_DIR above is to be replaced with the actual directory you created with wrangler generate.

Your wrangler.toml will look similar to this:

name = "wp2staticv6test"
type = "webpack"
account_id = "0a5e6f4c0d8a4e4cb4dd08d29e99ddcc"
workers_dev = true

[site]
bucket = "./public"

This example shows how to deploy to your workers.dev domain. Additional configuration is required to deploy to your own domain name. To be updated.