A simple maintenance mode plugin

Here’s a minuscule story: I’m ridding myself of a domain I started using for a sandbox in exchange for keeping jane.fyi, so I had to make a new sandbox—that is, a WordPress installation that’s online (because alas, I can’t do localhost on this Windows 10 laptop because the OS consumes so much storage). I need one so I can see the WordPress theme as I convert it from HTML to PHP theme files (what works best for me). I started with a new WordPress installation at a new sandbox location, which I’ll delete after I’m done with it (two themes), but didn’t want to go through the trouble of installing and configuring the maintenance mode plugin I’d been using.

Screenshot of "Sandbox" code

Anyway, I wanted something really basic, so I put a maintenance mode code snippet into plugin form. I can’t be bothered to rely on the functions.php file for whatever theme I’m using, so using it as a plugin seemed best. 🤷

Requirements

  • self-hosted WordPress (although it may work for Classic Press)
  • some HTML knowledge (maybe)

The code

If you just want to paste it into your theme’s functions.php file ’cause you know what you’re doing and need it for a moment, it is:

// Activate WordPress Maintenance Mode
function wp_maintenance_mode() {
    if (!current_user_can('edit_themes') || !is_user_logged_in()) {
        wp_die('<h1>Under Maintenance</h1>
        <p>Be back whenever.</p>');
    }
}
add_action('get_header', 'wp_maintenance_mode');

Editable parts

  • Change the text wrapped in the <h1> element to whatever you want
  • Change text wrapped in the <p> element to whatever you want

You can add more code, e.g.

// Activate WordPress Maintenance Mode
function wp_maintenance_mode() {
    if (!current_user_can('edit_themes') || !is_user_logged_in()) {
        wp_die('<h1>Sandbox</h1>
        <p>This is a sandbox that only I play in. Find me @</p>
        <ul>
            <li><a href="https://hej.gay">My personal blog</a></li>
            <li><a href="https://crunchyfamily.com">A collab blog</a></li>
            <li><a href="https://smoothiesbycolor.com">Smoothie recipes blog</a></li>
        </ul>');
    }
}
add_action('get_header', 'wp_maintenance_mode');

Just make sure you use the HTML code for any apostrophes you use (&#39;) and double quotes (“,”) for HTML requiring it.

Download as a plugin

I’ve seldom messed with Github releases, so I got off to a rocky start, but the final one (as far as I know) is v1.2. The plugin includes a precaution I apply to all my custom plugins, i.e. displaying a poop emoji to anyone who dares visit the direct URL of the file. 😏


As long as the plugin/theme with this snippet in it is active, the maintenance mode will be shown to anyone who visits any page of your website and is not logged in.

Support me by subscribing to my blog and/or buying me a cuppa:

Reply to Jamie

Comments on this post

Another nifty plugin that I happened to stumble upon during my adventures in “How can Jamie use this function if her functions.php file keeps messing up every time she tries something adventure.” I happened to stumble upon a plug-in (not sure if you’re aware of this but probably are?) called snippets plugin (it looks like a pair of scissors). What it does is enable the WordPress user or blogger access to his or her functions file without really having to add the code to your .php file, which could make or break your theme. This plugin I use on a daily basis (when I’m blogging that is), and has allowed me to actually use the thumbnail and other cool tricks as short codes. Now, if you want to style your code, you’re gonna have to have some knowledge of both HTML5 and CSS3.

Just thought I’d throw that plugin in there if you didn’t already know about it?

Reply to this »

Ah, relying on short codes is not good long-term practice. I try to use as few plugins as possible (less upkeep) and my own plugins where I can. I’m fine editing my functions.php file, no big. If you know what you’re doing, it’s not difficult to even fix when you “break” your site, thanks to control panels (e.g. cPanel).

I’d also be concerned re: security, but generally, my logic with plugins is to code it myself if I can—I don’t need an extra plugin for that.

Reply to this »

You’re absolutely right! However, i am not that great when it comes to coding behind the scenes I.e. plugins. I’m actually waiting patiently for Tristan to code me a content management system in Django because even he’s mad at WordPress for making things more difficult. I can’t wait. Who knows? Maybe it’ll be the next big CMS? Haha.

But yeah, I can code themes and what not but not plugins as I really didn’t get much from my C# levels 1 – 2 classes (I pretty much just got theory and one to two practices if anything. That class was new though even though it’s on my curriculum to get my associates degree.).

Reply to this »

ClassicPress is WordPress sans Gutenberg, if that’s what you mean by more difficult. Plugins aren’t that different from themes, they just require more PHP. I don’t know what C# has to do with it.

Reply to this »

haha I have to say I almost didn’t comment because I thought I landed in the wrong bog! WOW I’m impressed Jane! I’m not sure I would ever dare to try this since the last time I pasted something on mt theme’s file I crashed my blog 🙁

Reply to this »