Using WordPress ‘enable_maintenance_mode’ PHP filter

The enable_maintenance_mode WordPress PHP filter allows you to control the maintenance mode of your website.

Usage

add_filter('enable_maintenance_mode', 'your_custom_function', 10, 2);

function your_custom_function($enable_checks, $upgrading) {
    // your custom code here
    return $enable_checks;
}

Parameters

  • $enable_checks (bool) – Whether to enable maintenance mode. Default is true.
  • $upgrading (int) – The timestamp set in the .maintenance file.

More information

See WordPress Developer Resources: enable_maintenance_mode

Examples

Disable maintenance mode for administrators

Allow administrators to access the site while it’s in maintenance mode.

add_filter('enable_maintenance_mode', 'disable_maintenance_for_admins', 10, 2);

function disable_maintenance_for_admins($enable_checks, $upgrading) {
    if (current_user_can('administrator')) {
        return false;
    }
    return $enable_checks;
}

Allow specific IP addresses

Allow users with specific IP addresses to access the site during maintenance mode.

add_filter('enable_maintenance_mode', 'allow_specific_ips', 10, 2);

function allow_specific_ips($enable_checks, $upgrading) {
    $allowed_ips = array('192.0.2.1', '192.0.2.2');
    if (in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) {
        return false;
    }
    return $enable_checks;
}

Schedule maintenance mode

Enable maintenance mode only during specific hours of the day.

add_filter('enable_maintenance_mode', 'schedule_maintenance_mode', 10, 2);

function schedule_maintenance_mode($enable_checks, $upgrading) {
    $current_hour = date('H');
    if ($current_hour >= 1 && $current_hour <= 5) {
        return true;
    }
    return $enable_checks;
}

Disable maintenance mode for specific pages

Disable maintenance mode for specific pages on your website.

add_filter('enable_maintenance_mode', 'disable_maintenance_for_specific_pages', 10, 2);

function disable_maintenance_for_specific_pages($enable_checks, $upgrading) {
    $allowed_pages = array('about', 'contact');
    $current_page = basename($_SERVER['REQUEST_URI']);
    if (in_array($current_page, $allowed_pages)) {
        return false;
    }
    return $enable_checks;
}

Enable maintenance mode for specific user roles

Enable maintenance mode only for users with specific roles.

add_filter('enable_maintenance_mode', 'enable_maintenance_for_specific_roles', 10, 2);

function enable_maintenance_for_specific_roles($enable_checks, $upgrading) {
    if (current_user_can('subscriber')) {
        return true;
    }
    return $enable_checks;
}