18

How to Enable Maintenance Mode on Your WordPress Site for All Users Except Administrators

Powered by WPCodeBox

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;
    }
});
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.