1

I am using this code

add_action( 'wp_loaded', function() 
{
    global $pagenow;

    // - - - - - - - - - - - - - - - - - - - - - - 
    // Turn on/off you Maintenance Mode (true/false)
    define('IN_MAINTENANCE', true);
    // - - - - - - - - - - - - - - - - - - - - - - 

    if(
        defined( 'IN_MAINTENANCE' )
        && IN_MAINTENANCE
        && $pagenow !== 'wp-login.php'
        && ! is_user_logged_in()
    ) {
        header('HTTP/1.1 503 Service Temporarily Unavailable');
        header( 'Content-Type: text/html; charset=utf-8' );
        if ( file_exists( get_template_directory() . '/maintenance.php' ) ) {
            require_once( get_template_directory() . '/maintenance.php' );
        }
        die();
    }
});

in a custom plugin to temporarily put the site in maintenance mode. It works fine but I use cron job with external call to the wp-cron.php file. Is there any way to bypass maintenance mode only for the wp-cron.php file? Maybe I'm all wrong but thanks to anyone for any input!

1

You can use the wp_doing_cron() method to determine if the request comes from a cron request or not.

if(
    defined( 'IN_MAINTENANCE' )
    && IN_MAINTENANCE
    && $pagenow !== 'wp-login.php'
    && ! is_user_logged_in()
    && ! wp_doing_cron()
) {
3
  • 1
    Dear Kero, great, this works really well! So many thanks, so much luck. – Daniele As Nov 26 '20 at 17:38
  • Glad to hear. If this solved your question, please consider marking the answer as accepted (tick sign on the left of the answer) – kero Nov 26 '20 at 17:48
  • 1
    Done. I am new here. Thanks again. :-) – Daniele As Nov 26 '20 at 17:57

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.