X

Maintenance Mode for the Admin Only

Snippets by IsItWP

Do you want to put your WordPress admin section into maintenance mode without effecting the website? While there’s probably a plugin for this, we have created a quick code snippet that you can use to enable maintenance mode for the admin only in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function admin_maintenace_mode() {
    global $current_user;
    get_currentuserinfo();
    if($current_user->user_login != 'ADMIN_NAME') { ?>
			<style> .updated{margin:30px !important;} </style><?
			wp_die('<div id="message" class="updated"><p><b>Maintenance mode:</b> We are currently making updates. Everything will be online shortly.</p></div>');
		}
}
add_action('admin_head', 'admin_maintenace_mode');

Don’t forget to change the ADMIN_NAME in the above code to the username for your WordPress admin account.

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 50+ best responsive WordPress themes and how to create a custom user registration form in WordPress.

Comments   Leave a Reply

  1. Michael Hochleitner April 29, 2020 at 11:12 am

    Thank you Debjit Saha for the great snippet!

    I receive the following error when I paste it in my plugin.
    2020/04/29 13:08:35 [error] 1496#1496: *8 FastCGI sent in stderr: “PHP message: PHP ParseError: syntax error, unexpected end of file in /my/project/path/wp-content/plugins/a-plugin/a-plugin.php on line 55

    After changing the snippet to the following it worked without problems.

    function admin_maintenace_mode() {
    global $current_user;
    get_currentuserinfo();
    if($current_user->user_login != ‘ADMIN_NAME’) { ?>
    .updated{margin:30px !important;} <?php
    wp_die('Maintenance mode: We are currently making updates. Everything will be online shortly.’);
    }
    }
    add_action(‘admin_head’, ‘admin_maintenace_mode’);

    Best,
    Michael Hochleitner

  2. This should have been made user role based then it would have been more usable

    1. You could check the $current_user->user_level value in this case. Check out this snippet here: https://www.isitwp.com/redirect-user-after-login/

  3. wp-securehosting.com September 18, 2017 at 6:40 pm

    Thanks Kevin. I’m in a similar situation as squarecandy. I don’t want people modifying their site while I’m migrating them to my web-hosting company.

  4. Cool – thanks.
    This worked for me – I’m transitioning between servers and I didn’t want users editing their sites between when I started to copy everything over and when I made the switch to the new server.

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!