admin管理员组文章数量:1130349
To prevent my visitors from seeing a broken version of my site during maintenance, and to give them a heads up on the updates, I would like to redirect them automatically to a temporary maintenance page. I am looking for a portable solution which can be used on any site, without hardcoding URLs.
Logged in administrators (or other user level of choice) should get full access to the back-end and the front-end. There are a lot of plugins out there that offer this functionality, but I'm looking for a code-only solution.
To prevent my visitors from seeing a broken version of my site during maintenance, and to give them a heads up on the updates, I would like to redirect them automatically to a temporary maintenance page. I am looking for a portable solution which can be used on any site, without hardcoding URLs.
Logged in administrators (or other user level of choice) should get full access to the back-end and the front-end. There are a lot of plugins out there that offer this functionality, but I'm looking for a code-only solution.
Share Improve this question edited Jun 17, 2015 at 2:50 shea 5,6724 gold badges39 silver badges62 bronze badges asked Nov 26, 2014 at 13:37 NewUserNewUser 5572 gold badges9 silver badges19 bronze badges 3- copy the code from a plugin? – Mark Kaplun Commented Nov 26, 2014 at 13:56
- Why reinvent the wheel? Use a plugin. Specifically, Restricted Site Access. – vancoder Commented Nov 26, 2014 at 17:54
- 2 I try to use as less plugins as possible for my custom theme. "Less is more" ;) – NewUser Commented Nov 26, 2014 at 18:11
2 Answers
Reset to default 28WordPress has an embedded feature for handling maintenance mode.
When you upgrade a plugin, or WordPress core from WP dashboard, WordPress enters maintenance mode: it tries to load a file named maintenance.php located in the content folder (usually /wp-content), and if that file is not there, WP shows a default message.
I suggest you use that file, in this way you'll be consistent for your manually-triggered maintenance and for WordPress-handled maintenance.
How To
First of all create the
maintenance.phpfile and put there the content you want. For styling I suggest you put CSS in the file itself, using<style>tag; generally this is not good advice, but in this case it gives you the ability of using the file for WordPress-handled maintenance mode, when no theme is loaded (and the theme may be upgrading, so not reliable).Save the file just created in content folder (usually
/wp-content).In your
functions.phpput:add_action( 'wp_loaded', function() { global $pagenow; if( defined( 'IN_MAINTENANCE' ) && IN_MAINTENANCE && $pagenow !== 'wp-login.php' && ! is_user_logged_in() ) { header( 'HTTP/1.1 Service Unavailable', true, 503 ); header( 'Content-Type: text/html; charset=utf-8' ); header( 'Retry-After: 3600' ); if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) { require_once( WP_CONTENT_DIR . '/maintenance.php' ); } die(); } });This code will check a constant (see next point) and if user is not logged in, load the file created at point #1 and exit.
If you want to allow only users with specific capabilities, use
current_user_can('capability_to_allow')instead ofis_user_logged_in(). See Codex for more info.Maybe you can add to
maintenance.phpa link to login page; in this way a non-logged user can click it without having to manually enter the login URL in the address bar.If you are using a theme developed by a 3rd party, use a child theme; in this way you will be able to safely update the theme without losing your changes.
When you want to enable maintenance mode, open your
wp_config.phpand put there:define('IN_MAINTENANCE', true);After that, when you are ready to make your site public again, just remove that line or change
truetofalsefor easier re-enabling.
The previous answer is complete and well written. Anyhow if you are like me and you want to have everything in one place you can drop the following lines in the function.php file and create a maintenance.php file in your theme directory.
This is especially useful if your Git repository points just to the theme directory.
add_action( 'wp_loaded', function()
{
global $pagenow;
// - - - - - - - - - - - - - - - - - - - - - -
// Turn on/off you Maintenance Mode (true/false)
define('IN_MAINTENANCE', true);
// - - - - - - - - - - - - - - - - - - - - - -
if(
defined( 'IN_MAINTENANCE' )
&& IN_MAINTENANCE
&& $pagenow !== 'wp-login.php'
&& ! is_user_logged_in()
) {
header('HTTP/1.1 503 Service Temporarily Unavailable');
header( 'Content-Type: text/html; charset=utf-8' );
if ( file_exists( get_template_directory() . '/maintenance.php' ) ) {
require_once( get_template_directory() . '/maintenance.php' );
}
die();
}
});
NOTES
I changed the header to header('HTTP/1.1 503 Service Temporarily Unavailable'); as the one above didn't work for me.
To prevent my visitors from seeing a broken version of my site during maintenance, and to give them a heads up on the updates, I would like to redirect them automatically to a temporary maintenance page. I am looking for a portable solution which can be used on any site, without hardcoding URLs.
Logged in administrators (or other user level of choice) should get full access to the back-end and the front-end. There are a lot of plugins out there that offer this functionality, but I'm looking for a code-only solution.
To prevent my visitors from seeing a broken version of my site during maintenance, and to give them a heads up on the updates, I would like to redirect them automatically to a temporary maintenance page. I am looking for a portable solution which can be used on any site, without hardcoding URLs.
Logged in administrators (or other user level of choice) should get full access to the back-end and the front-end. There are a lot of plugins out there that offer this functionality, but I'm looking for a code-only solution.
Share Improve this question edited Jun 17, 2015 at 2:50 shea 5,6724 gold badges39 silver badges62 bronze badges asked Nov 26, 2014 at 13:37 NewUserNewUser 5572 gold badges9 silver badges19 bronze badges 3- copy the code from a plugin? – Mark Kaplun Commented Nov 26, 2014 at 13:56
- Why reinvent the wheel? Use a plugin. Specifically, Restricted Site Access. – vancoder Commented Nov 26, 2014 at 17:54
- 2 I try to use as less plugins as possible for my custom theme. "Less is more" ;) – NewUser Commented Nov 26, 2014 at 18:11
2 Answers
Reset to default 28WordPress has an embedded feature for handling maintenance mode.
When you upgrade a plugin, or WordPress core from WP dashboard, WordPress enters maintenance mode: it tries to load a file named maintenance.php located in the content folder (usually /wp-content), and if that file is not there, WP shows a default message.
I suggest you use that file, in this way you'll be consistent for your manually-triggered maintenance and for WordPress-handled maintenance.
How To
First of all create the
maintenance.phpfile and put there the content you want. For styling I suggest you put CSS in the file itself, using<style>tag; generally this is not good advice, but in this case it gives you the ability of using the file for WordPress-handled maintenance mode, when no theme is loaded (and the theme may be upgrading, so not reliable).Save the file just created in content folder (usually
/wp-content).In your
functions.phpput:add_action( 'wp_loaded', function() { global $pagenow; if( defined( 'IN_MAINTENANCE' ) && IN_MAINTENANCE && $pagenow !== 'wp-login.php' && ! is_user_logged_in() ) { header( 'HTTP/1.1 Service Unavailable', true, 503 ); header( 'Content-Type: text/html; charset=utf-8' ); header( 'Retry-After: 3600' ); if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) { require_once( WP_CONTENT_DIR . '/maintenance.php' ); } die(); } });This code will check a constant (see next point) and if user is not logged in, load the file created at point #1 and exit.
If you want to allow only users with specific capabilities, use
current_user_can('capability_to_allow')instead ofis_user_logged_in(). See Codex for more info.Maybe you can add to
maintenance.phpa link to login page; in this way a non-logged user can click it without having to manually enter the login URL in the address bar.If you are using a theme developed by a 3rd party, use a child theme; in this way you will be able to safely update the theme without losing your changes.
When you want to enable maintenance mode, open your
wp_config.phpand put there:define('IN_MAINTENANCE', true);After that, when you are ready to make your site public again, just remove that line or change
truetofalsefor easier re-enabling.
The previous answer is complete and well written. Anyhow if you are like me and you want to have everything in one place you can drop the following lines in the function.php file and create a maintenance.php file in your theme directory.
This is especially useful if your Git repository points just to the theme directory.
add_action( 'wp_loaded', function()
{
global $pagenow;
// - - - - - - - - - - - - - - - - - - - - - -
// Turn on/off you Maintenance Mode (true/false)
define('IN_MAINTENANCE', true);
// - - - - - - - - - - - - - - - - - - - - - -
if(
defined( 'IN_MAINTENANCE' )
&& IN_MAINTENANCE
&& $pagenow !== 'wp-login.php'
&& ! is_user_logged_in()
) {
header('HTTP/1.1 503 Service Temporarily Unavailable');
header( 'Content-Type: text/html; charset=utf-8' );
if ( file_exists( get_template_directory() . '/maintenance.php' ) ) {
require_once( get_template_directory() . '/maintenance.php' );
}
die();
}
});
NOTES
I changed the header to header('HTTP/1.1 503 Service Temporarily Unavailable'); as the one above didn't work for me.
本文标签: Redirect Visitors to a Temporary Maintenance Page
版权声明:本文标题:Redirect Visitors to a Temporary Maintenance Page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749207697a2332844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论