admin管理员组

文章数量:1314480

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!

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!

Share Improve this question asked Nov 26, 2020 at 16:30 Daniele AsDaniele As 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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()
) {

本文标签: Wp Maintenance mode and external cron job