WordPress website Maintenance Mode

Website maintenance mode is a feature in WordPress that allows you to temporarily disable certain features of your site while you perform maintenance or updates. This feature is especially useful for WordPress sites that experience high traffic or have a large number of users. By enabling maintenance mode, you can ensure that your site remains accessible to users while you work on it.

Creating the Maintenance Mode Function

To enable website maintenance mode in WordPress, you can use the following code in function.php:

function website_maintenance_mode(){
    if(!current_user_can('edit_themes') || !is_user_logged_in()){
		$message = '<div class="maintenance-mode">';
		$message .= '<span style="color:red">Website maintenance mode</span>';
		$message .= '<p>Please wait while we complete the maintenance.</p>';
		$message .= '</div>';

		wp_die($message, 'Maintenance Mode');
add_action('get_header', 'website_maintenance_mode');
  • function website_maintenance_mode() – This declares a function called website_maintenance_mode.
  • if (!current_user_can('edit_themes') || !is_user_logged_in()) – This checks two conditions:
    • current_user_can('edit_themes') – If the current user does not have the capability to edit themes.
    • is_user_logged_in() – If the user is not logged in.
      If either of these conditions is true, the function will execute the code inside the if statement.
  • wp_die($message, 'Maintenance Mode') – This function takes two arguments: the first is the message to display, and the second is the title of the page. In this case, the message is the $message variable, and the title is 'Maintenance Mode'. This function will display the message and then exit the page.

Enable or disable Maintenance Mode

This code can be added to your theme’s function.php file or to a plugin file. To enable or disable the maintenance mode as needed, you can use ACF Options Page by adding a checkbox there or using Options Page, more precisely the add_options_page() function.

In conclusion, website maintenance mode is a useful feature in WordPress that allows you to keep your site running smoothly during maintenance periods. By using ACF Options Page or Options Page, you can easily enable or disable the maintenance mode as needed. With these tips and tricks, you can keep your WordPress site running smoothly and avoid any downtime.