繁体   English   中英

主题内的 WordPress 维护页面

[英]WordPress maintenance page inside theme

我想知道是否有人知道在 WordPress 主题中使用 maintenance.php 文件的方法,而不是在 wp-content 文件夹中使用一个文件。

我主要是为functions.php 文件寻找一些代码,它会调用主题文件夹中的maintenance.php 文件。

我们想向维护页面添加一些品牌,因此最好能够从主题文件夹中使用它。 我知道有专门的插件可以解决这个问题。 但是我们不想让我们的网站从仅用于此类小细节的插件中增加太多开销,所以如果有办法通过主题文件夹实现这一点,那就太好了!

提前致谢!

当 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指向的任何位置)作为插入插件来在主题之外执行此操作。 当 WP 从wp_maintenance()内部检查维护模式时,它将查找该文件并加载它(如果存在),或者如果不存在则加载它自己的。 如果站点未处于维护模式,或处于其中的时间超过 10 分钟,即使该站点在技术上仍处于维护模式,“maintenance.php”也不会加载。 WordPress 4.6 引入了'enable_maintenance_mode'过滤器,它可以被wp-cli类的工具(ab)用来强制检查插件,并让您从维护文件中运行 CLI 命令。

Wordpress 切换维护模式

我们将创建的内容:

  • 用户- 显示“维护中”页面
  • 管理员- 能够查看整个网站
  • 向设置 - 常规面板添加一个选项以打开/关闭维护模式

首先,在您的主题根目录中创建一个maintenance.php文件:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>

<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <?php wp_head(); ?>
</head>

<body class="page-maintenance">

    <img src="<?= get_template_directory_uri() . '/assets/img/logo.png'; ?>" alt="<?= get_bloginfo('name') ?>">
    <p><?= get_bloginfo('description') ?></p>
    <h1>Under maintenance</h1>
    <p><b>We'll be back soon!</b></p>

    <?php wp_footer(); ?>
</body>
</html>

添加到functions.php

/** 
 * Under Maintenance
 */

// Add options checkbox to Settings / General 
function mythemename_settings_general_maintenance()
{
    add_settings_section(
        'my_settings_section', // Section ID 
        'ADDITIONAL SETTINGS', // Section Title
        'my_section_options_callback', // Content Callback
        'general' // Show under "General" settings page
    );
    add_settings_field(
        'maintenance_mode', // Option ID
        'Maintenance mode', // Option Label
        'maintenance_mode_callback', // Callback for Arguments 
        'general', // Show under "General" settings page
        'my_settings_section', // Name of the section
        array( // The $args to pass to the callback
            'maintenance_mode' // Should match Option ID
        )
    );
    register_setting('general', 'maintenance_mode', 'esc_attr');
}
function my_section_options_callback()
{
    // Custom Section Callback content
    echo "Custom theme options";
}
function maintenance_mode_callback($args)
{
    // Checkbox Callback
    $value = get_option($args[0]);
    $checked = ($value == "on") ? "checked" : "";
    echo "<label>
      <input type=\"checkbox\" id=\"$args[0]\" name=\"$args[0]\" $checked />
      <span>Check to activate Maintenance Mode page</span>
    </label><p>A general <i>Under Maintenance</i> page will be shown to non-admin users.</p>";
}
add_action('admin_init', 'mythemename_settings_general_maintenance');

// Handle Maintenance page
if (!function_exists('wp_under_maintenance')) :
    function wp_under_maintenance()
    {
        $isLoginPage = basename($_SERVER['PHP_SELF']) == 'wp-login.php';
        $isMaintenanceModeOn = get_option('maintenance_mode') == "on";

        if (
            $isMaintenanceModeOn &&
            !$isLoginPage &&
            !is_user_logged_in() &&
            !is_admin() &&
            !current_user_can("update_plugins")
        ) {
            get_template_part('maintenance');
            exit();
        }
    }
endif;
add_action('init', 'wp_under_maintenance', 30);

现在转到您的管理面板,设置,常规,您会发现:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM