Last updated on September 25, 2023

Enable Maintenance Mode for a Site

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Close for updates in SEO-friendly mode.

To enable maintenance mode for your WordPress site, you can use a code snippet that adds a simple check to redirect all non-admin users to a maintenance page. This can be useful when you need to perform updates or make changes to your site without affecting the user experience.

Here’s an example code snippet that you can add to your theme’s functions.php file or in a custom plugin:

function wpsnippets_maintenance_mode() {
    if ( ! current_user_can( 'manage_options' ) && ! is_admin() ) {
        wp_die( 'Under maintenance. Please check back soon.', 'Site Maintenance' );
    }
}
add_action( 'wp_loaded', 'wpsnippets_maintenance_mode' );

This code snippet checks if the current user is not an admin and is not accessing the admin area. If the condition is met, it displays a maintenance message using the wp_die() function, which stops the execution of the script and displays an error message.

You can customize the maintenance message by modifying the string passed as the first parameter to the wp_die() function.

Remember to remove or comment out this code snippet once you have finished the maintenance tasks to allow regular access to your site.

This code snippet can be useful when you want to temporarily restrict access to your site during maintenance or updates, ensuring that only authorized users can access the admin area.

Examples

Example 1: Enable Maintenance Mode using a Plugin

This use case demonstrates how to enable maintenance mode for a WordPress site using a plugin. The code example shows how to use the “WP Maintenance Mode” plugin to activate maintenance mode.

// Activate maintenance mode
function wpsnippets_enable_maintenance_mode() {
    if ( ! function_exists( 'is_plugin_active' ) ) {
        require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
    }

    if ( is_plugin_active( 'wp-maintenance-mode/wp-maintenance-mode.php' ) ) {
        activate_plugin( 'wp-maintenance-mode/wp-maintenance-mode.php' );
    }
}
add_action( 'init', 'wpsnippets_enable_maintenance_mode' );

In this code example, we first check if the is_plugin_active function exists. If not, we include the necessary file to use it. Then, we check if the “WP Maintenance Mode” plugin is active using the is_plugin_active function. If it is active, we activate the plugin using the activate_plugin function.

Example 2: Enable Maintenance Mode using a Custom Function

This use case demonstrates how to enable maintenance mode for a WordPress site using a custom function. The code example shows how to create a custom function that sets a maintenance mode flag in the database.

// Enable maintenance mode
function wpsnippets_enable_maintenance_mode() {
    update_option( 'wpsnippets_maintenance_mode', true );
}
add_action( 'init', 'wpsnippets_enable_maintenance_mode' );

In this code example, we create a custom function wpsnippets_enable_maintenance_mode that updates the value of the wpsnippets_maintenance_mode option to true. This flag can be used to determine whether the site is in maintenance mode or not. We then hook this function to the init action to enable maintenance mode.

Example 3: Enable Maintenance Mode using a Maintenance Page Template

This use case demonstrates how to enable maintenance mode for a WordPress site using a maintenance page template. The code example shows how to create a custom page template that displays a maintenance message.

<?php
/*
Template Name: Maintenance Page
*/

// Check if maintenance mode is enabled
if ( get_option( 'wpsnippets_maintenance_mode' ) ) {
    // Display maintenance message
    echo '<h1>Site Under Maintenance</h1>';
    echo '<p>We are currently performing maintenance on our site. Please check back later.</p>';
} else {
    // Display regular content
    while ( have_posts() ) : the_post();
        the_content();
    endwhile;
}
?>

In this code example, we create a custom page template named “Maintenance Page” by adding a comment with the template name at the beginning of the file. We then check if maintenance mode is enabled by checking the value of the wpsnippets_maintenance_mode option. If it is enabled, we display a maintenance message. Otherwise, we display the regular content of the page.

Last updated on September 25, 2023. Originally posted on October 15, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *