Activating WordPress maintenance mode from wp-cli / shell

Sometimes you want your WordPress site to go into maintenance mode while working behind the scenes with wp-cli from the shell.

Like when you are updating themes, plugins, or even WordPress core itself. Or do some work on the database, importing entries, things like that. Before you’re finished, the site might look broken or be in an inconsistent state. Something the visitor should not see.

Unfortunately, wp-cli does not bring a command to control the maintenance mode.

WordPress would require you to add a .maintenance file and inside that even add some PHP code for the timing. Not what I want to do. I like to keep it simple. I want a one line command that does the job. And one that is as simple to deactivate the maintenance mode again.

And this is how it works for me:

Nice. But what is this and how does it work?

I use a plug-in, called Ultimate Maintenance Mode. Inside this plug-in I have previously enabled the maintenance mode. And leave the enable option always to yes:

Of course we don’t always want the site to show a maintenance mode page. It won’t:

By activating or deactivating the mentioned plug-in we can now control the display of a maintenance message accordingly. Which makes it simple to do from the shell with wp-cli.

As you can see, I’m flushing my caches after activating and also when deactivating the plug-in / maintenance mode.

I do the first to not have any cache entries in my database and filesystem to keep the backup size smaller. Doing this is optional and a decision for yourself.

The flushing after deactivating the plug-in is mendatory, though. It will avoid that maintenance pages show up in WordPress page caches like the one of W3 Total Cache I am using.

If you use some kind of frontend caching in addition, you should not need to worry, as the plugin is sending a HTTP 503: Service Unavailable status code.