How to use WordPress code maintenance mode without plugins.

In this article, you will learn how to add WordPress maintenance mode code. We explain how to easily implement this code. We will also show you an alternative option, a plugin that allows you to activate maintenance mode.
Zdjęcie profilowe autora

Bartosz Swinitsky

In this article, you will learn how to add WordPress maintenance mode code. We explain how to easily implement this code. We will also show you an alternative option, a plugin that allows you to activate maintenance mode.

How to add maintenance mode in WordPress using code?

One of the easiest ways to enable maintenance mode in WordPress is to add special PHP code to the file functions.php Your theme. This code blocks access to the site for non-logged-in users, displaying them a message about ongoing technical work:

function wp_maintenance_mode() {
    if (!current_user_can('edit_themes') || !is_user_logged_in()) {
        wp_die('<h1>The site is under maintenance</h1><p>Please join us soon!</p>');
    }
}
add_action('get_header', 'wp_maintenance_mode');
Is it worth adding code to the functions.php file?

WordPress code maintenance mode can be easily added to the file functions.php, but ... this solution has a serious drawback. The functions.php file is directly linked to your theme. This means that When updating the theme, all manually added codes will be removed. As a result, the maintenance code will be lost, and you will have to add it from scratch.

A better alternative: Plugin Code Snippets

Instead of adding the code directly to the file functions.php, I recommend using the Code Snippets plugin. This tool allows you to add custom code snippets without the risk of losing them when updating the theme.

Why use Code Snippets?
  • The codes are stored independently of the theme.
  • There is no risk of removing them during the upgrade.
  • You can easily manage and edit the added code snippets.
  • The plug-in has an intuitive interface, making adding code simple even for beginners.
How to add WordPress maintenance mode code using Code Snippets?
  1. Install the plug-in Code Snippets From the WordPress repository.
  2. Go to tab Snippets In the admin panel.
  3. Click Add New (Add New).
  4. Paste the maintenance code and give it a name, such as "Maintenance Mode."
  5. Save and activate the snippet.
Summary

Adding the code to the functions.php file is possible, but I recommend using the Code Snippets plugin. This is a safer and more future-proof solution. With this plugin, your codes won't be lost even after a theme update.

If you prefer ready-made tools, you can also use plug-ins such as WP Maintenance Mode, which automate the process of activating maintenance mode.

Scroll to Top