PHPwordpressbeginner
WordPress Maintenance Mode
Put your WordPress site in maintenance mode while keeping admin access
Faisal Yaqoob
November 8, 2025
#wordpress#maintenance#development#coming-soon
Code
php
1 // Enable maintenance mode for non-admin users 2 function custom_maintenance_mode() { 3 // Allow admin access 4 if (current_user_can('edit_themes') || is_user_logged_in()) { 5 return; 6 } 7
8 // Exclude admin and login pages 9 if (is_admin() || $GLOBALS['pagenow'] === 'wp-login.php') { 10 return; 11 } 12
13 // Set proper HTTP header 14 wp_die( 15 get_maintenance_mode_html(), 16 'Maintenance Mode', 17 array('response' => 503) 18 ); 19 } 20 add_action('get_header', 'custom_maintenance_mode'); 21
22 // Maintenance mode HTML 23 function get_maintenance_mode_html() { 24 ob_start(); 25 ?> 26 <!DOCTYPE html> 27 <html <?php language_attributes(); ?>> 28 <head> 29 <meta charset="<?php bloginfo('charset'); ?>"> 30 <meta name="viewport" content="width=device-width, initial-scale=1"> 31 <meta name="robots" content="noindex, nofollow"> 32 <title><?php bloginfo('name'); ?> - Under Maintenance</title> 33 <style> 34 * { 35 margin: 0; 36 padding: 0; 37 box-sizing: border-box; 38 } 39
40 body { 41 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; 42 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 43 color: #fff; 44 display: flex; 45 align-items: center; 46 justify-content: center; 47 min-height: 100vh; 48 padding: 20px; 49 } 50
51 .maintenance-container { 52 text-align: center; 53 max-width: 600px; 54 } 55
56 .maintenance-icon { 57 font-size: 80px; 58 margin-bottom: 30px; 59 animation: pulse 2s infinite; 60 } 61
62 @keyframes pulse { 63 0%, 100% { opacity: 1; } 64 50% { opacity: 0.5; } 65 } 66
67 h1 { 68 font-size: 48px; 69 margin-bottom: 20px; 70 font-weight: 700; 71 } 72
73 p { 74 font-size: 20px; 75 margin-bottom: 15px; 76 opacity: 0.9; 77 } 78
79 .countdown { 80 background: rgba(255, 255, 255, 0.1); 81 border-radius: 10px; 82 padding: 20px; 83 margin-top: 30px; 84 backdrop-filter: blur(10px); 85 } 86
87 .social-links { 88 margin-top: 40px; 89 } 90
91 .social-links a { 92 color: #fff; 93 margin: 0 10px; 94 font-size: 24px; 95 text-decoration: none; 96 opacity: 0.8; 97 transition: opacity 0.3s; 98 } 99
100 .social-links a:hover { 101 opacity: 1; 102 } 103 </style> 104 </head> 105 <body> 106 <div class="maintenance-container"> 107 <div class="maintenance-icon">🔧</div> 108 <h1>We'll Be Back Soon!</h1> 109 <p><?php bloginfo('name'); ?> is currently undergoing scheduled maintenance.</p> 110 <p>We apologize for any inconvenience.</p> 111
112 <div class="countdown"> 113 <p><strong>Expected completion:</strong> <?php echo date('F j, Y'); ?></p> 114 </div> 115
116 <div class="social-links"> 117 <!-- Add your social media links here --> 118 <a href="#" aria-label="Facebook">📘</a> 119 <a href="#" aria-label="Twitter">🐦</a> 120 <a href="#" aria-label="Instagram">📷</a> 121 </div> 122 </div> 123 </body> 124 </html> 125 <?php 126 return ob_get_clean(); 127 }
WordPress Maintenance Mode
Display a maintenance mode page to visitors while allowing administrators to access and work on the site normally.
// Enable maintenance mode for non-admin users
function custom_maintenance_mode() {
// Allow admin access
if (current_user_can('edit_themes') || is_user_logged_in()) {
return;
}
// Exclude admin and login pages
if (is_admin() || $GLOBALS['pagenow'] === 'wp-login.php') {
return;
}
// Set proper HTTP header
wp_die(
get_maintenance_mode_html(),
'Maintenance Mode',
array('response' => 503)
);
}
add_action('get_header', 'custom_maintenance_mode');
// Maintenance mode HTML
function get_maintenance_mode_html() {
ob_start();
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex, nofollow">
<title><?php bloginfo('name'); ?> - Under Maintenance</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
padding: 20px;
}
.maintenance-container {
text-align: center;
max-width: 600px;
}
.maintenance-icon {
font-size: 80px;
margin-bottom: 30px;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
h1 {
font-size: 48px;
margin-bottom: 20px;
font-weight: 700;
}
p {
font-size: 20px;
margin-bottom: 15px;
opacity: 0.9;
}
.countdown {
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
padding: 20px;
margin-top: 30px;
backdrop-filter: blur(10px);
}
.social-links {
margin-top: 40px;
}
.social-links a {
color: #fff;
margin: 0 10px;
font-size: 24px;
text-decoration: none;
opacity: 0.8;
transition: opacity 0.3s;
}
.social-links a:hover {
opacity: 1;
}
</style>
</head>
<body>
<div class="maintenance-container">
<div class="maintenance-icon">🔧</div>
<h1>We'll Be Back Soon!</h1>
<p><?php bloginfo('name'); ?> is currently undergoing scheduled maintenance.</p>
<p>We apologize for any inconvenience.</p>
<div class="countdown">
<p><strong>Expected completion:</strong> <?php echo date('F j, Y'); ?></p>
</div>
<div class="social-links">
<!-- Add your social media links here -->
<a href="#" aria-label="Facebook">📘</a>
<a href="#" aria-label="Twitter">🐦</a>
<a href="#" aria-label="Instagram">📷</a>
</div>
</div>
</body>
</html>
<?php
return ob_get_clean();
}
Activation Toggle
// Enable/disable maintenance mode with a constant
if (!defined('MAINTENANCE_MODE')) {
define('MAINTENANCE_MODE', true); // Set to false to disable
}
function custom_maintenance_mode_toggle() {
if (!MAINTENANCE_MODE) {
return;
}
if (current_user_can('manage_options') || is_admin()) {
return;
}
wp_die(get_maintenance_mode_html(), 'Maintenance Mode', array('response' => 503));
}
add_action('template_redirect', 'custom_maintenance_mode_toggle');
With IP Whitelist
// Allow specific IPs to access the site
function maintenance_mode_with_whitelist() {
$allowed_ips = array(
'192.168.1.1',
'10.0.0.1',
// Add your IPs here
);
$user_ip = $_SERVER['REMOTE_ADDR'];
// Skip maintenance for whitelisted IPs
if (in_array($user_ip, $allowed_ips)) {
return;
}
// Skip for logged-in admins
if (current_user_can('manage_options')) {
return;
}
// Skip admin pages
if (is_admin()) {
return;
}
wp_die(get_maintenance_mode_html(), 'Maintenance Mode', array('response' => 503));
}
add_action('template_redirect', 'maintenance_mode_with_whitelist');
Coming Soon Mode (Before Launch)
// Coming soon page with email signup
function coming_soon_mode() {
if (current_user_can('manage_options') || is_admin()) {
return;
}
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Coming Soon - <?php bloginfo('name'); ?></title>
<style>
body {
font-family: Arial, sans-serif;
background: #1a1a1a;
color: #fff;
text-align: center;
padding: 50px 20px;
}
h1 {
font-size: 60px;
margin-bottom: 20px;
}
p {
font-size: 24px;
margin-bottom: 40px;
}
.email-form {
max-width: 500px;
margin: 0 auto;
}
input[type="email"] {
width: 70%;
padding: 15px;
font-size: 16px;
border: none;
border-radius: 5px 0 0 5px;
}
button {
width: 30%;
padding: 15px;
font-size: 16px;
background: #4CAF50;
color: white;
border: none;
border-radius: 0 5px 5px 0;
cursor: pointer;
}
button:hover {
background: #45a049;
}
</style>
</head>
<body>
<h1>Coming Soon</h1>
<p>Our new website is under construction</p>
<div class="email-form">
<form action="#" method="post">
<input type="email" name="email" placeholder="Enter your email" required>
<button type="submit">Notify Me</button>
</form>
</div>
</body>
</html>
<?php
exit(ob_get_clean());
}
add_action('template_redirect', 'coming_soon_mode');
Admin Notice
// Show notice to admins when maintenance mode is active
function maintenance_mode_admin_notice() {
if (!current_user_can('manage_options')) {
return;
}
if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE) {
echo '<div class="notice notice-warning is-dismissible">';
echo '<p><strong>Maintenance Mode Active:</strong> Your site is currently showing a maintenance page to visitors.</p>';
echo '</div>';
}
}
add_action('admin_notices', 'maintenance_mode_admin_notice');
Features
- Admin Access: Administrators can work normally
- Professional Design: Clean, modern maintenance page
- SEO Friendly: Proper 503 HTTP status code
- IP Whitelist: Allow specific IPs to bypass
- Easy Toggle: Enable/disable with a constant
- Customizable: Easy to modify design and content
Related Snippets
Register Custom Post Type
Complete example of registering a custom post type with all common options
PHPwordpressintermediate
phpPreview
function register_portfolio_post_type() {
$labels = array(
'name' => _x('Portfolio Items', 'Post type general name', 'textdomain'),
'singular_name' => _x('Portfolio Item', 'Post type singular name', 'textdomain'),
...#custom-post-type#wordpress#development+1
1/9/2025
View
WordPress Custom Breadcrumbs
Create SEO-friendly breadcrumb navigation without plugins
PHPwordpressintermediate
phpPreview
// Display breadcrumbs
function custom_breadcrumbs() {
// Settings
$separator = ' » ';
...#wordpress#breadcrumbs#navigation+2
11/15/2025
View
Create Custom WordPress REST API Endpoint
Add custom REST API endpoints to extend WordPress functionality
PHPwordpressintermediate
phpPreview
// Register custom REST API endpoint
function register_custom_api_endpoint() {
register_rest_route('custom/v1', '/posts', array(
'methods' => 'GET',
...#wordpress#rest-api#api+2
11/14/2025
View