Home > database >  With Wordpress in Maintenance Mode, how do we exclude the wp-login.php page?
With Wordpress in Maintenance Mode, how do we exclude the wp-login.php page?

Time:10-19

So we added a custom maintenance page to our custom wordpress theme using ACF - but we somehow have to exclude the wp-login.php page or we won't be able to login to our website if Maintenance is on, any suggestions on how we would do this? Here is the code we have so far:

/////////////  Wordpress Maintenance Mode  ///////////

function maintenance_mode() {
    $context = Timber::get_context();
    Timber::render('maintenance.twig', $context);
    die();
}

$config = get_field('website', 'option');
if($config["status"]) {
    add_filter( 'wp_die_handler', '_custom_wp_die_handler' );
}else {
    if(!is_user_logged_in() && !is_admin()) {
        maintenance_mode();
    }

}

function _custom_wp_die_handler( $message, $title = '', $args = array() ) {
    maintenance_mode();
}

CodePudding user response:

Using $wp->request you should be able to add an exclusion for the wp-login page to your if(!is_user_logged_in() && !is_admin()) { statement to prevent the code from loading there.

You also need to use home_url( $wp-request ) inside a function call. The code for this would be something like:

add_action('pre_get_posts', 'maintenance_mode_logic');
function maintenance_mode_logic() {
    global $wp;
    $config = get_field('website', 'option');
    if($config["status"] == 1) {
        echo 'hello';
        exit();
        add_filter( 'wp_die_handler', '_custom_wp_die_handler' );
    }else {
        if(!is_user_logged_in() && !is_admin() && home_url( $wp->request ) != wp_login_url()) {
            maintenance_mode();
        }
     
    }
}
  • Related