WordPress维护模式功能

时间:2019-03-02 08:16:02

标签: wordpress

我想在满足条件时在wordpress中启用维护模式。我想永久为所有人启用此模式,并每天2次按cron检查状况。只有我可以手动将其关闭。我尝试了不同的解决方案,但他们不想工作。我目前拥有的代码

function do_this_hourly() {

if(2>1){

    // Activate WordPress Maintenance Mode
function wp_maintenance_mode() {
    if (!current_user_can('edit_themes') || !is_user_logged_in()) {
        wp_die('<h1>Under Maintenance</h1><br />Something aint right, but were working on it! Check back later.');
    }
}
add_action('get_header', 'wp_maintenance_mode');
} else {
    echo "page work";
}
}

1 个答案:

答案 0 :(得分:0)

更新:

function wp_maintenance_mode() {
    // Extra, don't run if WordPress is doing updates
    if ( file_exists( ABSPATH . '.maintenance' ) || wp_installing() ) {
        return;
    }

    // Activate Maintenance Mode
    if (!current_user_can('edit_themes') || !is_user_logged_in()) {
        wp_die('<h1>Under Maintenance</h1><br />Something aint right, but were working on it! Check back later.');
    }
}

add_action('get_header', 'wp_maintenance_mode');


// Disable the maintenance if your condition is met.
if( you_condition ) {
    remove_action('get_header', 'wp_maintenance_mode');
}

此外,您可以像这样检查cron是否正在进行:

if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
    // code to be run if there is a cron in progress
}