WordPress

How to Set WordPress Website in Maintenance Mode

At times, you may need to take your WordPress website offline for maintenance or updates. During this time, it’s important to display a maintenance message to your visitors, letting them know that your site is temporarily unavailable. Fortunately, you can easily set your WordPress website in maintenance mode without using a plugin by adding a few lines of code to your `functions.php` file.

Why and When you should set WordPress website in maintenance mode?

Updating Plugins and Themes: One of the most common reasons for setting a WordPress website in maintenance mode is to update plugins and themes. Updating your site’s plugins and themes is important for security, performance, and bug fixes. However, these updates can sometimes cause issues, and it’s best to perform them when your site is in maintenance mode so that your visitors are not affected.

Site Upgrades: If you’re upgrading your WordPress site to a new version, you may need to take your site offline for a short period of time. During this time, you can set your site in maintenance mode to inform your visitors that your site is temporarily unavailable.

Site Maintenance: From time to time, you may need to perform site maintenance, such as fixing broken links, cleaning up your database, or optimizing your site’s performance. During this time, you can set your site in maintenance mode to prevent your visitors from accessing your site while you work on it.

Scheduled Maintenance: If you’re performing scheduled maintenance, you should set your site in maintenance mode ahead of time. This allows you to inform your visitors that your site will be unavailable, and gives them time to plan accordingly.

Unplanned Maintenance: If you need to perform unplanned maintenance, such as fixing a security issue or fixing a broken plugin, you should set your site in maintenance mode as soon as possible. This will prevent your visitors from accessing your site while you work on it.

Steps to set wordpress website in maintenance mode.

Please note that do the customization in your child theme. If you haven’t yet created child theme then read this article to create one. If you do not want to edit functions.php file then install a plugin Code Snippets by Code Snippets Pro, and add the code by using this plugin.

Step 1: Access functions.php file.

Go to Appearance > Theme File Editor > Theme Functions (functions.php). Below is the image for your reference.

how to access theme file editor
how to access theme functions or functions.php

Step 2: Add below code.

You’ll need to add the following code to your theme’s functions.php file. This code checks if the user is not logged in and displays the maintenance mode page to all visitors except logged-in users.

//set website in maintainence mode
function wpamit_maintenance_mode() {
    if (!current_user_can('edit_themes') || !is_user_logged_in()) {
        wp_die('<h1 style="text-align:center;">Maintenance Mode</h1><p style="text-align:center;">Our website is currently in maintenance mode. Please check back later.</p>', 'Maintenance Mode');
    }
}
add_action('get_header', 'wpamit_maintenance_mode');

Note: You can customize the maintenance mode message by changing the text highlighted line.

Step 3: Test the Maintenance Mode

Finally, test the maintenance mode by logging out of your WordPress site. You should see the maintenance mode page that you created in Step 1. To exit maintenance mode, simply log back in to your WordPress site.

Please note: If you are using cache plugin then clear cache before testing.

Conclusion

Setting your WordPress website in maintenance mode is a simple process that can be accomplished without a plugin by adding a few lines of code to your `functions.php` file. This can be useful if you need to take your site offline for maintenance or updates, and it allows you to display a custom message to your visitors while your site is in maintenance mode.

More Articles:
Change WordPress Login Logo without Plugin.
Restore WordPress Classic Widget Editor.
Change WordPress Admin Dashboard Footer Text.

Leave a Reply

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