最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Making an under maintenance page (without using plugins)

programmeradmin2浏览0评论

There are a bazillion plugins that does this. And every time I use one, I end up spending hours trying to style the message (and logo and background) to fit the styling of the page.

So I was wondering if I could make one easily myself, by using this function:

function custom_maintenance_page() {
  if ( ! is_user_logged_in() ) {
    if( $_SERVER['REQUEST_URI'] != '/under-maintenance/' ){
      wp_redirect( '' );
    }
  }
}
add_action( 'template_redirect', 'custom_maintenance_page' );

And then making a new page, with a custom page template, with the 'Under Maintenance'-message. And also, - I could add some CSS like this:

body.page-template-page-under-maintenance {

  header,
  footer {
    display: none !important;
  }
}

Upsides:

  • The styling for the page is the same, so Google Analytics, stylings and all that are the same.
  • It's quick and easy to implement.
  • It's not relying on 3rd party code.
  • The customer can still work on the site, when they're logged in.

Downsides

  • People would be able to remove the display: none and see the header and footer. However, - if the visit other pages, then they're redirected to the same one, since they're not logged in.
  • The API is still open.

My question is... Am I missing an obvious flaw in this? Or would this work alright?

There are a bazillion plugins that does this. And every time I use one, I end up spending hours trying to style the message (and logo and background) to fit the styling of the page.

So I was wondering if I could make one easily myself, by using this function:

function custom_maintenance_page() {
  if ( ! is_user_logged_in() ) {
    if( $_SERVER['REQUEST_URI'] != '/under-maintenance/' ){
      wp_redirect( 'https://example/under-maintenance' );
    }
  }
}
add_action( 'template_redirect', 'custom_maintenance_page' );

And then making a new page, with a custom page template, with the 'Under Maintenance'-message. And also, - I could add some CSS like this:

body.page-template-page-under-maintenance {

  header,
  footer {
    display: none !important;
  }
}

Upsides:

  • The styling for the page is the same, so Google Analytics, stylings and all that are the same.
  • It's quick and easy to implement.
  • It's not relying on 3rd party code.
  • The customer can still work on the site, when they're logged in.

Downsides

  • People would be able to remove the display: none and see the header and footer. However, - if the visit other pages, then they're redirected to the same one, since they're not logged in.
  • The API is still open.

My question is... Am I missing an obvious flaw in this? Or would this work alright?

Share Improve this question asked May 1, 2020 at 20:16 ZethZeth 9282 gold badges13 silver badges43 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Add this to your header.php file.

if(!is_user_logged_in()) {
    wp_die('This site is currently undergoing maintenance.');
}

You can replace This site is currently undergoing maintenance. with whatever message you want to show.

发布评论

评论列表(0)

  1. 暂无评论