How to Enable Maintenance Mode in WordPress: A Step-by-Step Guide with Code Snippets

If you’re planning to update your WordPress site or perform any major changes, enabling maintenance mode is a smart way to minimize any disruptions to your visitors. By enabling maintenance mode, you can display a customized message or page that informs visitors that your site is temporarily offline while you make updates or perform maintenance tasks.

In this tutorial, we’ll show you how to enable maintenance mode in WordPress using two different methods: creating a maintenance page or using the wp_die() function. First, we’ll walk you through the process of creating a custom maintenance page that visitors will see when maintenance mode is enabled. Then, we’ll provide a code snippet that you can add to your site’s functions.php file to automatically redirect visitors to the maintenance page using the wp_die() function.

Whether you’re a WordPress beginner or an experienced user, this guide will help you enable maintenance mode quickly and easily. Let’s get started!

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Option 1: Create a Custom Maintenance Page

Step 1: Create a Maintenance Page

To create a custom maintenance page in WordPress, follow these steps:

  1. Log in to your WordPress dashboard and click on “Pages”.
  2. Click on “Add New” to create a new page.
  3. Give your page a title, such as “Maintenance Mode” or “Site Under Maintenance”.
  4. Add your custom message to the page, explaining why your site is in maintenance mode and when it will be back online.
  5. Add images or other media to the page using the WordPress editor.
  6. Publish the page or save it as a draft.

Step 2: Add the Maintenance Mode Function to Your Functions.php File

To add the maintenance mode function to your WordPress theme’s functions.php file, use the following code snippet:

function wp_maintenance_mode() {
	if ( ! current_user_can( 'edit_themes' ) || ! is_user_logged_in() ) {
		$maintenance_page_url = 'https://example.com/maintenance-page'; // Replace with the URL of your maintenance page.
		wp_safe_redirect( $maintenance_page_url );
		exit;
	}
}
add_action( 'get_header', 'wp_maintenance_mode' );
Code language: PHP (php)

Option 2: Use the wp_die() Function

Step 1: Add the Maintenance Mode Function to Your Functions.php

File To use the wp_die() function to put your site in maintenance mode, add the following code snippet to your WordPress theme’s functions.php file:

function wp_maintenance_mode() {
	if ( ! current_user_can( 'edit_themes' ) || ! is_user_logged_in() ) {
		$message = 'This site is currently in maintenance mode. Please try again later.';
		wp_die( $message, 'Maintenance Mode', array( 'response' => 503 ) );
	}
}
add_action( 'get_header', 'wp_maintenance_mode' );
Code language: PHP (php)

That’s it! Putting your WordPress site in maintenance mode is crucial for ensuring that your site remains functional and accessible to visitors. Using a custom maintenance page or the wp_die() function are both simple and easy ways to put your site in maintenance mode. By following the steps outlined in this tutorial, you can put your WordPress site in maintenance mode in just a few simple steps.

Leave your feedback and help us improve šŸ¶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ā˜•ļø or donating via PayPal šŸ’°.

More free knowledge, because why not?

Your thoughts matter, leave a reply šŸ’¬

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