604

How to Enable Maintenance Mode on Your WordPress Site for Non-Admins

This WordPress snippet enables maintenance mode, displaying a customizable message to all site visitors except administrators. Easily integrate this code to temporarily take your WordPress site offline for updates or maintenance, ensuring a smooth user experience.


<?php

add_action('template_redirect', function() {
    if( !current_user_can( 'manage_options') && !is_admin()) {
        status_header(503);
        ?>
        <h1 style="font-family:system-ui;text-align:center;margin-top:100px;">
        The site is currently in maintenance mode. Check back soon!
        </h1>
        <?php
        exit;
    }
});

Quick Steps to Enable WordPress Maintenance Mode for Non-Admins

Follow these simple steps to enable maintenance mode for non-admin users:

  1. Install and activate the WPCodeBox plugin, then navigate to WPCodeBox 2 in your WordPress dashboard.
  2. Copy the code snippet above and paste it into the WPCodeBox code editor.
  3. Customize the maintenance message in the code to match your needs (optional).
  4. Give your snippet a descriptive title (like “Maintenance Mode”), select PHP as the type, then click Save.
  5. Enable the snippet by toggling the switch to the active position.
  6. Test the maintenance mode by logging out and visiting your site to see the maintenance page.

That’s it! Your site is now in maintenance mode for all non-administrator users.

Enable WordPress Maintenance Mode for Non-Admins – Additional Information

What is WordPress Maintenance Mode

Maintenance mode is a feature that temporarily displays a custom page to visitors while you work on your site. Instead of showing broken pages, error messages, or incomplete updates, visitors see a professional message explaining that your site is undergoing maintenance.

The code snippet provided sends a proper HTTP 503 status code, which tells search engines that your site is temporarily unavailable and they should check back later. This is important for SEO because it prevents search engines from thinking your site has permanently disappeared or encountered serious errors.

When to Use Maintenance Mode

You should enable maintenance mode during several scenarios. Major plugin or theme updates are perfect times to use it, as these changes can temporarily break your site’s functionality or appearance. WordPress core upgrades also require maintenance mode, especially when moving between major versions.

Database migrations or repairs are another critical time to use maintenance mode. These operations can make your site unstable or inaccessible while they’re running. Site redesigns benefit from maintenance mode too, allowing you to implement new layouts and features without visitors seeing the work in progress.

Best Practices for Maintenance Mode

When implementing maintenance mode, timing is important. Try to schedule maintenance during low-traffic periods to minimize disruption to your visitors. Keep downtime as short as possible, ideally under 30 minutes for routine updates.

Your maintenance message should be clear and professional. Include information about what’s happening and when visitors can expect the site to be back online. If maintenance will take longer, consider adding contact information or social media links so visitors can stay connected.

Always test your changes on a staging site first if possible. This helps you identify potential issues before they affect your live site. Make sure to backup your site before enabling maintenance mode, especially for major updates.

We also have a video in which we use this snippet:

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.