Sarathlal N

Add a temporary maintenance session for WordPress site

In some critical situations, we want to shut down our site for a limited time. It may be a maintenance time or any other error resolving session.

In such occasions, adding a maintenance screen & 503 error page is always better option for our visitors.

We can achieve this by adding a simple function in WordPress.

Just add below function in our child theme’s function.php file.

<?php
// Temp Maintenance - with http response 503 (Service Temporarily Unavailable)
// This will only block users who are NOT an administrator from viewing the website.
function wp_maintenance_mode(){
	if(!current_user_can('edit_themes') || !is_user_logged_in()){
		wp_die('Maintenance, please come back soon.', 'Maintenance - please come back soon.', array('response' => '503'));
	}
}
add_action('get_header', 'wp_maintenance_mode');
?>

After adding this function, it will add a 503 error page for all users in front end. Same time, we can access back-end & can login as user in back-end. But the administrator can only see front end until we remove this function from our function.php.

So after the maintenance session, we want to remove this function to make our WordPress as live again.

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.