Simple 2 steps to have your WordPress website to Maintenance Mode without a plugin

Christos January 30, 2023
simple-2-steps-to-have-your-wordpress-website-to-maintenance-mode-without-a-plugin

WordPress Maintenance Mode is required when we want to make major changes to our WordPress site (custom code and/or database changes), which require that the front end of the site be unavailable to visitors.

WordPress Maintenance Mode without Plugins

Add this code to your theme's functions.php file.
The site (both the Admin panel and the front end) is only accessible to the administrator(s) with this code. Anyone else will see the message "Maintenance Mode."

Enable WordPress Maintenance Mode

/**
 * Maintenance mode
 */
function wp_maintenance_mode() {
 
    if ( ! current_user_can( 'administrator' ) ) {
        wp_logout();
    }
 
    wp_die( '

Website under Scheduled Maintenance


Please check back soon.' ); } /** * Check if current page is the login page * * @return bool */ function is_wp_login() { if ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { return true; } return false; } if ( false === is_user_logged_in() && false === is_wp_login() ) { add_action( 'get_header', 'wp_maintenance_mode' ); } else { if ( ! current_user_can( 'administrator' ) ) { add_action( 'admin_init', 'wp_maintenance_mode' ); } }

Disable WordPress Maintenance Mode

Simply comment out the following lines (or remove the entire if block):

if ( false === is_user_logged_in() && false === is_wp_login() ) {
    //add_action( 'get_header', 'wp_maintenance_mode' );
} else {
    if ( ! current_user_can( 'administrator' ) ) {
        //add_action( 'admin_init', 'wp_maintenance_mode' );
    }
}

WordPress Maintenance Mode With Plugin

You can use a variety of plugins.

In this tutorial we use the LightStart – Maintenance Mode, Coming Soon and Landing Page Builder
Install and activate the plugin.
You may want to use many of the available options: Menu » Settings » LighStart

Remember to Purge the Cache (if any), otherwise, the plugin may not work.

also view other related posts,