PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 26, 2022

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

 August 26, 2022     maintenance-mode, php, wordpress, wordpress-theming     No comments   

Issue

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!


Solution

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. 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.

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.

If you use the wp_die function, you'll get the standard white box on grey background. 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. 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. 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. 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.



Answered By - Exprator
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing