0

I am trying to use this function to add maintenance mode feature by adding this in my theme's functions.php and I am trying to use my theme options to enable/disable it.

I realized when i check/uncheck this via theme options page, it doesnt work. My guess is, once the action is added, it cannot be disabled. I tried to add elseif condition with remove_action for the same hook, but that didn't work either..

        function mytheme_under_construction(){

            // if user is logged in, don't show the construction page
            if ( is_user_logged_in() ) {
                return;
            }
            $protocol = $_SERVER["SERVER_PROTOCOL"];
            if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
                $protocol = 'HTTP/1.0';
            // 503 is recommended :  http://bit.ly/YdGkXl
            header( "$protocol 503 Service Unavailable", true, 503 );
            // or header( "$protocol 200 Ok", true, 200 );
            header( 'Content-Type: text/html; charset=utf-8' );
            // adjust the Retry-After value (in seconds)
            header( 'Retry-After: 3600' );

        ?>
        <?php get_template_part('maintanance'); ?>
        <?php

            die();      
        }

global $mytheme;
if(!empty($mytheme['offline_id'])){
add_action( 'template_redirect', 'amytheme_under_construction' );
}

The onyl way it works is when I direclty add/remove the ! from this line:

if(!empty($mytheme['offline_id'])){

Can anyone help me with the issue?

Thanks

  • Could you add var_dump($mytheme) just after global $mytheme? What does it output? Also show us, pls, how do you register and set your options. – Krzysiek Dróżdż Nov 26 '13 at 7:19
  • Just fixed my issue, by running the theme options condition inside the function. Thanks for your effort :) – Towfiq Nov 26 '13 at 7:52
0

Actually all I had to do is, run theme options inside the function, like this:

        function mytheme_under_construction(){
        global $mytheme;
        if(!empty($mytheme['offline_id'])){
            // if user is logged in, don't show the construction page
            if ( is_user_logged_in() ) {
                return;
            }
            $protocol = $_SERVER["SERVER_PROTOCOL"];
            if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
                $protocol = 'HTTP/1.0';
            // 503 is recommended :  http://bit.ly/YdGkXl
            header( "$protocol 503 Service Unavailable", true, 503 );
            // or header( "$protocol 200 Ok", true, 200 );
            header( 'Content-Type: text/html; charset=utf-8' );
            // adjust the Retry-After value (in seconds)
            header( 'Retry-After: 3600' );

        ?>
        <?php get_template_part('maintanance'); ?>
        <?php

            die();      
        }
}
add_action( 'template_redirect', 'amytheme_under_construction' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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