How to Enable Maintenance Mode in WordPress

By ThemesDNA2 min read

Share:

When performing updates or changes to your WordPress site, it's essential to inform visitors that the site is temporarily unavailable. While many plugins can handle this, you can manually enable maintenance mode by adding a custom PHP snippet to your theme. This allows for greater control and flexibility.

The following code can be added to your theme's functions.php file to enable maintenance mode for non-admin users, while still allowing administrators and critical processes (like AJAX requests, cron jobs, and REST API calls) to function normally.

function themesdna_maintenance_mode() {
    // Allow access for admins and specific non-front-end requests
    if ( current_user_can( 'manage_options' ) || is_admin() || wp_doing_ajax() || wp_doing_cron() || defined( 'REST_REQUEST' ) && REST_REQUEST ) {
        return; // Allow access for admin, AJAX, CRON, or REST requests
    }

    // Maintenance mode message
    $maintenance_message = '<h1>Maintenance Mode</h1><p>We are performing scheduled maintenance. Please check back soon.</p>';

    // Output the maintenance mode message for non-admin users
    wp_die( $maintenance_message, 'Maintenance Mode', array( 'response' => 503 ) );
}

// Enable Maintenance Mode for non-admin users
add_action( 'template_redirect', 'themesdna_maintenance_mode' );

By using this code, you can control exactly who sees the maintenance page and ensure your site remains operational behind the scenes while you perform updates.

Premium WordPress themes Made for WordPress   /   Ready for your story

Ready to Build?

Your next great website starts here.

Build more websites with one flexible collection—and keep every theme for life.

  • 56+ themes
  • Lifetime updates
  • Unlimited websites
  • No renewals

The complete collection

$75

One payment. No renewals.

Get the Themes Bundle — $75