Want to put your website temporarily under maintenance mode? You don’t need a plugin for that, a few lines of code will do the trick.
Whatever your reason is for putting your WordPress site into maintenance mode, this snippet is perfect. You can move your site, change themes, perform database maintenance, you name it.
It’s also great for new sites that are still under development. Just change the on-screen message to something like: “This site is still under maintenance.”
Don’t worry that you won’t be able to access your own site anymore after using this snippet. In the if-statement the code runs two checks to see whether the current user is logged in or not. If you are, you won’t see this message and can work on your site without other people being able to see the site at all.
Visitors will see a simple message to reflect the maintenance mode of your site. You can edit the message within the wp_die(‘ ‘) brackets.
function wp_maintenance_mode() {
if ( ! current_user_can('edit_themes') || ! is_user_logged_in() ) {
wp_die('<h1>Under Maintenance</h1><br>
We are currently performing scheduled maintenance. We will be back shortly!
');
}
}
add_action('get_header', 'wp_maintenance_mode');