简体   繁体   English

主题内的 WordPress 维护页面

[英]WordPress maintenance page inside theme

I was wondering if anyone knows a way to use a maintenance.php file inside your WordPress theme, instead of one in the wp-content folder.我想知道是否有人知道在 WordPress 主题中使用 maintenance.php 文件的方法,而不是在 wp-content 文件夹中使用一个文件。

I'm mainly looking for some code for the functions.php file which would call the maintenance.php file in the theme folder.我主要是为functions.php 文件寻找一些代码,它会调用主题文件夹中的maintenance.php 文件。

We'd like to add some branding to the maintenance page and therefore it'd be best to be able to use it from inside the theme folder.我们想向维护页面添加一些品牌,因此最好能够从主题文件夹中使用它。 I know there are special plugins for this.我知道有专门的插件可以解决这个问题。 But we don't want to give our sites too much overhead from plugins that are only used for small details like this, so if there's a way to achieve this via the theme folder it'd be great!但是我们不想让我们的网站从仅用于此类小细节的插件中增加太多开销,所以如果有办法通过主题文件夹实现这一点,那就太好了!

Thanks in advance!提前致谢!

When WordPress goes into maintenance mode, it adds a file named .maintenance to the root directory while the maintenance is being performed, then it's removed afterwards.当 WordPress 进入维护模式时,它会在执行维护时将一个名为.maintenance的文件添加到根目录,然后将其删除。 You can write a function inside your theme's functions.php that checks for this file and loads a custom maintenance page from the theme.您可以在主题的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' );
}

Put your maintenance content in the maintenance.php page inside your theme folder and you're all set to style it however you would like.将您的维护内容放在主题文件夹内的maintenance.php页面中,然后您就可以随意设置样式了。

If you use the wp_die function, you'll get the standard white box on grey background.如果您使用wp_die函数,您将获得灰色背景上的标准白框。 This way lets you style your maintenance page like you would any other theme page.通过这种方式,您可以像设计任何其他主题页面一样设置维护页面的样式。

You can also do this outside the theme by adding maintenance.php to the wp-content directory (or wherever you've set WP_CONTENT_DIR to point to) as a drop-in plugin.您还可以通过将maintenance.php添加到wp-content目录(或您设置WP_CONTENT_DIR指向的任何位置)作为插入插件来在主题之外执行此操作。 When WP checks for maintenance mode from inside wp_maintenance() it'll look for that file and load it if present, or load its own if not.当 WP 从wp_maintenance()内部检查维护模式时,它将查找该文件并加载它(如果存在),或者如果不存在则加载它自己的。 If the site isn't in maintenance mode, or is in it for more than 10 minutes, 'maintenance.php' won't load even though the site is technically still in maintenance mode.如果站点未处于维护模式,或处于其中的时间超过 10 分钟,即使该站点在技术上仍处于维护模式,“maintenance.php”也不会加载。 WordPress 4.6 introduces the 'enable_maintenance_mode' filter, which can be (ab)used by a tool like wp-cli to force the check for the drop-in and would let you run a CLI command from your maintenance file. WordPress 4.6 引入了'enable_maintenance_mode'过滤器,它可以被wp-cli类的工具(ab)用来强制检查插件,并让您从维护文件中运行 CLI 命令。

Wordpress switch Maintenance mode Wordpress 切换维护模式

What we'll create:我们将创建的内容:

  • User - show the "Under Maintenance" page用户- 显示“维护中”页面
  • Admin - able to view the entire website管理员- 能够查看整个网站
  • Add an option to the Settings - General panel to switch the maintenance mode on/off向设置 - 常规面板添加一个选项以打开/关闭维护模式

First, create a maintenance.php file in your theme root:首先,在您的主题根目录中创建一个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>

Add to functions.php :添加到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);

Now go to your Admin panel, Settings, General and you'll find:现在转到您的管理面板,设置,常规,您会发现:

在此处输入图片说明

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

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