brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wp maintenance-mode puts your site on hold during a big change

WordPress
~/tips/tip-wordpress-wp-maintenance-mode

wp maintenance-mode activate

Turns WordPress maintenance mode on and off from the command line.

When I do something bigger to a WordPress site, like a database search-replace or a batch of plugin updates, I don’t want visitors clicking around a half-changed site. WP-CLI has a command for maintenance mode, so you don’t need a plugin for it.

wp maintenance-mode activate
# do the work
wp maintenance-mode deactivate

The command has four subcommands: activate, deactivate, status and is-active. status prints a line you can read, and is-active answers with an exit code, so a script can check it. In the WP-CLI docs example, is-active returns 1 on a site that isn’t in maintenance.

A small script I’d use around updates:

wp maintenance-mode activate
wp plugin update --all
wp maintenance-mode deactivate
wp maintenance-mode status

The last line is there so you see with your own eyes that the site is back.

Caveat: if the script stops halfway, nothing switches maintenance off again. The site stays on hold until you run deactivate yourself, so in a script, make sure the deactivate step runs even when an update fails. There’s also a --force option on activate; the docs don’t say much more about it, so I’d leave it alone unless a plain activate refuses.

Further reading: WP-CLI: wp maintenance-mode

More WordPress tips wp user application-password create anna backup-script --porcelain define( 'MEDIA_TRASH', true ); wp cli check-update wp theme delete --all wp media image-size All WordPress tips (85) All topics

Link