Halloween Sale 15% off Ends in See the plans
PHP

Network-Wide Maintenance Mode Switch

Adds a checkbox to Network Admin > Settings that puts every site on the network into maintenance mode at once; super admins can still browse and work normally.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! is_multisite() ) {
    return;
}

add_action( 'wpmu_options', function () {
    ?>
    <h2><?php esc_html_e( 'Maintenance' ); ?></h2>
    <table class="form-table" role="presentation">
        <tr>
            <th scope="row"><?php esc_html_e( 'Maintenance mode' ); ?></th>
            <td><label><input type="checkbox" name="wpide_network_maintenance" value="1" <?php checked( get_site_option( 'wpide_network_maintenance' ) ); ?>> <?php esc_html_e( 'Show a maintenance page on every site to everyone except super admins' ); ?></label></td>
        </tr>
    </table>
    <?php
} );

add_action( 'update_wpmu_options', function () {
    if ( current_user_can( 'manage_network_options' ) ) {
        update_site_option( 'wpide_network_maintenance', empty( $_POST['wpide_network_maintenance'] ) ? 0 : 1 );
    }
} );

add_action( 'template_redirect', function () {
    if ( ! get_site_option( 'wpide_network_maintenance' ) || is_super_admin() ) {
        return;
    }

    header( 'Retry-After: 3600' );
    wp_die(
        '<h1>' . esc_html__( 'Down for maintenance' ) . '</h1><p>' . esc_html__( 'We are making some improvements and will be back shortly.' ) . '</p>',
        esc_html__( 'Maintenance' ),
        array( 'response' => 503 )
    );
} );

add_action( 'admin_bar_menu', function ( $bar ) {
    if ( get_site_option( 'wpide_network_maintenance' ) && is_super_admin() ) {
        $bar->add_node( array(
            'id'    => 'wpide-network-maintenance',
            'title' => '<span style="color:#f0b849">' . esc_html__( 'Network maintenance ON' ) . '</span>',
            'href'  => network_admin_url( 'settings.php' ),
        ) );
    }
}, 100 );

Notes

Visitors get a 503 response with a Retry-After header, which tells search engines the downtime is temporary.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.