How to use a maintenance.php file inside a WordPress theme?

Viewed 1318

I was wondering if anyone knows a way to use a maintenance.php file inside your WordPress theme, instead of the one in the wp-content folder.

I'm mainly looking for some code for the functions.php file which would call the maintenance.php file in the theme folder.

We'd like to add some branding to the maintenance page and therefore it would 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!

2 Answers

Wordpress switch Maintenance mode

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:

<!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:

/** 
 * 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:

enter image description here

Related