45

How to Implement a Maintenance Mode with a Secret Code in WordPress

Powered by WPCodeBox

Learn how to implement a maintenance mode in WordPress with a secret code to restrict access to your website.


add_action('get_header', function() {
 
    if(!current_user_can('manage_options')){
        
        // You can change the below secret code to whatever you want. This will let your customer see the website if the add to the link the following /?maint={YOUR SECRET CODE}
        
        if($_GET['maint'] == 'SKASDJAS') {
            
            setcookie('Maintenance', $_GET['maint'], time() + (86400 * 30), "/");
            
        } elseif($_COOKIE['Maintenance'] == 'SKASDJAS') {
            
            
        } else {
            
            wp_die('<h1> Maintenance mode</h1><br> The site is in maintenance mode, <br>please check back later.', 
            'The site is in maintenance mode, please check back later.', array('response' => '503'));
            
        }
        

    }
});

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.