Galanthus
Galanthus

Reputation: 2290

wp_maintenance_mode - Boolean True/False not working

I have created a define where I am able to enable and disable the WP maintenance mode with true or false. Logged in users and people who can edit the theme are able to visit the site.

But for some reason.. the function will always return "false". even with true and false set.

Am I missing something out?

Code:

<?php

// Enable maintenance mode
define('NM_ENABLE_MAINTENANCE_MODE', false);

if(NM_ENABLE_MAINTENANCE_MODE && !current_user_can('edit_themes') || !is_user_logged_in()) {
    function wp_maintenance_mode() {
        wp_die('<h1>' . NM_MAINTENANCE_TITLE . '</h1>' . '<p>' . NM_MAINTENANCE_MESSAGE) . '</p>';
    }
}


add_action('get_header', 'wp_maintenance_mode');

Upvotes: 1

Views: 355

Answers (1)

hppycoder
hppycoder

Reputation: 1026

I look at this as a truth table. You have three variables A,B,C and the current statement says (A ∧ B ∨ C) which isn't what you want. Instead the truth table for (A ∧ (B ∨ C)) is what you want. That would say IF NM_ENABLE_MAINTENANCE_MODE is true AND user cannot edit themes OR user is not logged in SHOW the page. So they could be logged in but NOT be allowed to edit themes then they should see the page.

A = Constant for NM_ENABLE_MAINTENANCE_MODE

B = INVERSE of current_user_can('edit_themes') (so cannot edit themes)

C = INVERSE of is_user_logged_in() (so not logged in)

A   B   C   SHOW PAGE
T   T   T   T
T   T   F   T
T   F   T   T
T   F   F   F
F   T   T   F
F   T   F   F
F   F   T   F
F   F   F   F

I believe the code you are looking for is (function only if IF is true):

<?php

// Enable maintenance mode
define('NM_ENABLE_MAINTENANCE_MODE', false);

if(NM_ENABLE_MAINTENANCE_MODE && (!current_user_can('edit_themes') || !is_user_logged_in())) {
    function wp_maintenance_mode() {
        wp_die('<h1>' . NM_MAINTENANCE_TITLE . '</h1>' . '<p>' . NM_MAINTENANCE_MESSAGE) . '</p>';
    }
    add_action('get_header', 'wp_maintenance_mode');
}

Code if you want the function to always be available:

<?php

// Enable maintenance mode
define('NM_ENABLE_MAINTENANCE_MODE', false);

function wp_maintenance_mode() {
   if(NM_ENABLE_MAINTENANCE_MODE && (!current_user_can('edit_themes') || !is_user_logged_in())) {
        wp_die('<h1>' . NM_MAINTENANCE_TITLE . '</h1>' . '<p>' . NM_MAINTENANCE_MESSAGE) . '</p>';
    }
}
add_action('get_header', 'wp_maintenance_mode');

EDIT: Moved the add_action inside of the IF statement so you don't get the undefined function error.

Upvotes: 1

Related Questions