To hide a WordPress site until it is ready, you can use a maintenance mode plugin or implement a custom code snippet. The code snippet below demonstrates how to create a simple maintenance mode page that displays a message to visitors while hiding the rest of the site’s content.
function wpsnippets_maintenance_mode() {
if ( ! current_user_can( 'edit_themes' ) || ! is_user_logged_in() ) {
wp_die( 'This website is currently undergoing maintenance. Please check back soon.' );
}
}
add_action( 'template_redirect', 'wpsnippets_maintenance_mode' );
This code should be added to your theme’s functions.php
file or a custom plugin file. It checks if the current user can edit themes (to allow administrators access) or if the user is logged in. If neither condition is met, it uses the wp_die()
function to display a maintenance mode message and prevent access to the rest of the site.
Please note that this code snippet only provides a basic maintenance mode functionality. If you need more advanced features, such as a countdown timer or a custom maintenance page, you may want to consider using a dedicated maintenance mode plugin.
Examples
Example 1: Using a Plugin to Hide WordPress Site
This use case demonstrates how to use a plugin to hide your WordPress site until it is ready for public access. The code example shows how to use the “Coming Soon Page & Maintenance Mode” plugin to achieve this functionality.
// Install and activate the "Coming Soon Page & Maintenance Mode" plugin
Explanation: Install and activate the “Coming Soon Page & Maintenance Mode” plugin to enable a coming soon page or maintenance mode for your WordPress site. This plugin allows you to easily customize the page and restrict access to your site until it is ready.
Example 2: Using a Custom Function to Hide WordPress Site
This use case demonstrates how to use a custom PHP function to hide your WordPress site until it is ready. The code example shows how to create a custom function using the wp_die()
function to display a maintenance message.
function wpsnippets_hide_site() {
wp_die( 'Site under maintenance. Please check back later.' );
}
add_action( 'template_redirect', 'wpsnippets_hide_site' );
Explanation: Create a custom function wpsnippets_hide_site()
that uses the wp_die()
function to display a maintenance message when someone tries to access your WordPress site. The template_redirect
action hook is used to trigger the function and prevent access to the site.
Example 3: Using a Maintenance Mode Plugin to Hide WordPress Site
This use case demonstrates how to use a maintenance mode plugin to hide your WordPress site until it is ready. The code example shows how to use the “WP Maintenance Mode” plugin to achieve this functionality.
// Install and activate the "WP Maintenance Mode" plugin
Explanation: Install and activate the “WP Maintenance Mode” plugin to enable a maintenance mode for your WordPress site. This plugin provides various customization options to create a maintenance page and restrict access to your site until it is ready.