When you update your website, perform changes, or fix bugs, it is important to temporarily hide your site from visitors.
A properly implemented maintenance mode page ensures users see a friendly message instead of a broken website.
In this article, you will learn how to create a custom maintenance mode using WordPress hooks without installing plugins.
Why Maintenance Mode Is Important
Showing a broken or unfinished site can damage trust and SEO performance.
- Prevents users from seeing errors
- Protects SEO rankings during updates
- Improves user experience
- Allows safe development and testing
The WordPress Hook We Will Use
We will use:
template_redirect
This hook runs before the page is fully loaded, making it perfect for redirecting users to a maintenance page.
Example Code
add_action('template_redirect', 'custom_maintenance_mode');
function custom_maintenance_mode() {
if (current_user_can('manage_options')) {
return;
}
if (!is_user_logged_in()) {
wp_die(
'Maintenance Mode
We are currently performing scheduled maintenance. Please check back soon.
',
'Maintenance Mode',
array('response' => 503)
);
}
}
How It Works
- Checks if the user is an admin
- Allows admins to access the site normally
- Blocks all other users
- Displays a maintenance message
Advanced Improvements
- Create a full custom maintenance template
- Allow access by IP address
- Add countdown timer until site reopens
- Collect emails for notifications
Disable Maintenance Mode for Specific Pages
if (is_page('contact')) {
return;
}
