Maintenance mode excluding site administrators

I want to set the site into maintenance mode without using a plugin so that anyone who accesses the site who is not a site admin will see a sorry site under maintenance page.

I created a .maintenance file with the code below:

?php
function is_user_logged_in() {
    $loggedin = false;
    foreach ( (array) $_COOKIE as $cookie = $value ) {
        if ( stristr($cookie, 'wordpress_logged_in_') )
            $loggedin = true;
    }
    return $loggedin;
}
if ( ! stristr($_SERVER['REQUEST_URI'], '/wp-admin')  
     ! stristr($_SERVER['REQUEST_URI'], '/wp-login.php')  
     ! is_user_logged_in() )
    $upgrading = time();
?

I also customized a maintenance.php file with page content and installed it in wp-content.

That way it worked well, but the problem is that anyone who is already logged into the site can browse the site. As it's an ecommerce site, it's common for customers to have cookies installed, but I don't want them to be able to use the site while it's under maintenance.

I'm not a developer, so I don't know how to fix the code above to let only admins get around maintenance mode.

Can someone help me?

Edit:

I'm going to use this during the begining of Black Friday to update store prices and my goal is to load as little WordPress as possible when people come to the site so as not to overload the server. In previous years the site didn't handle the high volume of simultaneous users very well while trying to activate promotions and this year, in addition to other actions, I want to prevent people who are constantly refreshing the page from affecting the performance of WordPress.

In my view, the .maintenance file method makes the user to load very little WordPress resources. But please correct me if I'm wrong.

Topic maintenance admin Wordpress

Category Web


Only administrators already login will have access to the website, no one else. just change the $url variable to your url where you have the maintenance page desired.

function wp_maintenance_mode() {
if (!current_user_can('administrator')) {
   $url = "your maintenance file url you want to display while is on maintenance";
   wp_redirect( $url );
   exit;
}
add_action('get_header', 'wp_maintenance_mode');

if you just want to display a message just remove the redirect and echo something

function wp_maintenance_mode() {
   if (!current_user_can('administrator')) {
      wp_die('<h1>Under Maintenance</h1><br />Website under planned maintenance. Please check back later.');
   }
}
add_action('get_header', 'wp_maintenance_mode');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.