WordPress Snippets at WPcustoms

Enable maintenance mode for non-admins

Put your site in maintenance mode. Only administrators can access the site.. Nice solution if you need to change specific technical. You can also change the return into any other http response code. I.e. if you permanently removed your site to another domain and don’t want to put it into htaccess simply use this snippet with a 302 permament redirect.


/**
 * Snippet Name: Enable maintenance mode for non-admins
 * Snippet URL: https://wpcustoms.net/snippets/enable-maintenance-mode-non-admins/
 */
  // http response 503 (Service Temporarily Unavailable)
// block users who are NOT an administrator from viewing the website.
function wpc_maintenance_mode(){
    if(!current_user_can('edit_themes') || !is_user_logged_in()){
        wp_die('Maintenance, please come back soon.', 'Maintenance - please come back soon.', array('response' => '503'));
    }
}
add_action('get_header', 'wpc_maintenance_mode');