問題描述

有沒有辦法從主題文件夾添加自定義維護頁面?

我有激活維護模式的代碼,但我無法得到樣式..

function activate_maintenance_mode() {
    if ( !(current_user_can( 'administrator' ) ||  current_user_can( 'super admin' ))) {
        wp_die(
        '<h1>Website Under Maintenance</h1><p>Hi, our Website is currently undergoing scheduled maintenance.
        Please check back very soon.<br /><strong>Sorry for the inconvenience!</strong></p>', 'Maintenance Mode');
    }
}
add_action('get_header', 'activate_maintenance_mode');

先謝謝,尼古拉。

最佳解決方案

當 WordPress 進入維護模式時,會在執行維護時將一個名為.maintenance 的文件添加到根目錄中,之後刪除。您可以在主題的 functions.php 中編寫一個功能,用於檢查此文件,並從主題加載自定義維護頁面。

if ( ! function_exists( 'wpse84987_maintenance_mode' ) ) {
    function wpse84987_maintenance_mode() {
        if ( file_exists( ABSPATH . '.maintenance' ) ) {
            include_once get_stylesheet_directory() . '/maintenance.php';
            die();
        }
    }
    add_action( 'wp', 'wpse84987_maintenance_mode' );
}

將您的維護內容放在您的主題文件夾中的 maintenance.php 頁面中,您將全部設置為您想要的樣式。

如果使用 wp_die 功能,您將得到灰色背景上的標準白色框。這樣,您可以像任何其他主題頁面一樣為維護頁面設置樣式。

更新:您還可以通過將 maintenance.php 添加到 wp-content 目錄 (或者將 WP_CONTENT_DIR 設置為指向的位置) 作為 drop-in 插件,從而在主題之外執行此操作。當 WP 從 wp_maintenance()內部檢查維護模式時,它將尋找該文件並將其加載 (如果存在),或者如果不存在則加載它。如果站點未處於維護模式,或者存在 10 分鐘以上,即使站點在技術上仍處於維護模式,’maintenance.php’ 將無法加載。 WordPress 4.6 introduces the ‘enable_maintenance_mode’ filter,它可以被 (ab) 使用,像 wp-cli 這樣的工具強制檢查 drop-in,並允許您從維護文件運行 CLI 命令。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。