0

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.

1 Answer 1

0

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');
6
  • Thank you very much for the reply, Tiago! As I understand it, I would have to add this function to my functions.php, right? Would there be any way to do this through the method of the .maintenance file? Commented Nov 12, 2021 at 8:58
  • yes, in functions.php. what is your .maintenace file, is it a page that people land with the info that is on maintenance design the way you wanted? if so, find the url to display that page and just simply change the $url variable on the first code of this answer to that url. example $url = "htttps://google.com"; this will use your hidden maintenance file. Commented Nov 12, 2021 at 19:09
  • When WordPress updates a plugin, for example, it creates a hidden file in the root of the site called ".maintenance". When this file is active, it blocks access to the site with a message "Briefly unavailable for scheduled maintenance. Check back in a minute.". You can customize the content of this message using a file called "maintenance.php" in the wp-content folder. This page talks about it: hostpapasupport.com/… Commented Nov 15, 2021 at 12:14
  • With an internet tutorial, I managed to customize the code of the ".maintenance" file so that logged in users can still access the site as I said in the question. The problem is that I can't limit this access to administrators only. Commented Nov 15, 2021 at 12:14
  • I am understanding now, didnt knew about he maintenance.php, its good to catch something new. in that way had never done it, so.. I was doing makes your website on maintenance mode, also if you want redirects all traffic except admin to a specific page. Commented Nov 15, 2021 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.