5 snippets for replacing WordPress coming soon & maintenance plugins

In my last post on WPCodebox  I said I’d share the snippets I’m gathering to reduce my dependency on WordPress plugins.

You don’t need WPCodebox here as you can simply add these php snippets to your child theme’s functions.php file. But, it’s worth checking out as it’s great for efficiency and does much more. In fact, for me it’s main benefit is allowing me to bring sassy CSS in my WordPress work-flow.

Additionally, I want to thank the plugin’s author, Ovidiu, for his ongoing support and for helping me to fix one of my own coding attempts included in this post  (I am not a programmer, but I find it really empowering working with code ).

Context

The “traditional” coming soon page is something I don’t need as much with my move to a more agile, data-driven approach.  The aim with that is to get something out fast that might give us early SEO benefits and provide user behaviour data. This, then guides the ongoing design and potentially allows the site to start paying for it’s own design, but, of course, we always need ways to hide what is not ready.


WPCodeBox –  25% off coupon

Just enter “beaverjunction” to get the best discount available.


1. Activate the WordPress maintenance mode

WordPress dynamically generates a maintenance page when we are updating themes and plugins. Activating this mode is the easiest way to hide our work.

The snippet here is taken from the WPCodebox repository. I’ve changed  it to “edit_pages (from manage_options) to allow editors in. Also I’ve added a little HTML and tried to make it clear which part is for the page title.

add_action('get_header', function() {
if(!current_user_can('edit_pages')){

wp_die('<h1>NOTHING HERE!</h1>
<p>This is a private test site.</p>', 
'This shows on the browser', array('response' => '503'));
}
});
/* change "edit_pages" to "manage_options" to block all but admins
 and use "read" to unblock all logged in users*/

This is best used for quick maintenance  jobs and temporary domains where we want search engines to ignore us. It returns a 503 “Service Unavailable” code.

When I move on to content using Sassy CSS via WPCodebox to globally style my sites I’ll do something on styling the maintenance mode. Here, it does not matter it’s ugly. It’s only job is to hide the site.

My use:  Website redesigns and my YouTube videos.  It can also get used on new project.  With sprint based work the risk is low for clients and they pay even before a domain name is researched and finalized. To keep momentum I may start and show work in videos, but I may not want them (or their colleagues) looking up the URL and jumping ahead of the process.

2. Redirect to a “coming soon” page

This snippet redirects non-logged in users to a “coming soon” page (ie, www.mysite.com/coming-soon). You need a page with a WordPress URL slug that matches the one in the code. It does the same as the Restricted Site Access plugin which some suggest for coming soon pages.

I would change “coming soon” to a URL that could live on after the site has gone live to make use of any benefit gained from it being indexed by search engines and potentially linked to.

add_action("template_redirect", "my_custom_coming_soon");

function my_custom_coming_soon()
{
if (!is_user_logged_in() && !is_page("coming-up")) {
wp_redirect(site_url("coming-up"));

exit();
}
}

The benefits over a stand-alone coming soon plugins is you can use your own builder, SEO and caching plugin, but you will need to hide your headers and footers to non logged-in visitors.  Some themes allow you to remove them conditionally –  which is the ideal (the Beaver Builder Theme and Beaver Themer in my case). If not you could try to hide them visually with the CSS code snippets I’ve included below.

My use:  I would use this for a landing page where there is an early offer. Perhaps only one product is publicly ready.  More simply it could get used as a contact page.

Additionally, it could be used to tell visitors what is happening when doing some site changes. WPCodebox makes it easy as you can just download one of your snippets from the cloud and turn it on or off.

3. Redirect to a “coming soon” page and allow other pages to be public

This is the same as above but allows you to make other pages public. You need to change the page in the public page array to you own page slugs.

add_action('template_redirect', 'coming_soon_redirect');

function coming_soon_redirect()
{
global $pagenow;

$public_pages = array("thank-you", "offer", "downloads");

if(!is_user_logged_in() && !is_page($public_pages) && $page_now != "wp-login.php")
{
wp_redirect(home_url("coming-up"));
exit;
}
}
// include the slug used in the redirect in the public pages too

 

My use:   The same as above, but also to send visitors to a thank you or downloads page.

4. Redirect to the “home”

Self explanatory. Non-logged in users will only get to see the homepage.  There is no code to change here and as before,  you would need to hide the header and footer.  Having the root of the domain indexed by search engines seems the ideal to me.

add_action( 'template_redirect', 'home_coming_soon' );
function home_coming_soon() {
if( !is_user_logged_in() && ! (is_front_page() || is_home()) ){
wp_redirect( site_url() );
exit();
}
}

My use:  This is useful if the client is convinced of the benefits of getting a minimal viable website out. My present process is influenced by UX designers. Typically it starts with research that leads to creating copy for a homepage (which can often work as a landing page). The visual design is then about how to illustrate the story the content tells whist trying to reduce cognitive friction.

If the client is not concerned about strangers discovering a bare-bones design on their new domain this approach is likely to speed up their work on SEO and conversions.

5. Redirect to “home” and allow other pages to be public

As above , but with access to more pages.

add_action('template_redirect', 'dw_homepage_redirect');

function dw_homepage_redirect()
{
$public_pages = array( "thank-you", "offer", "downloads");

if(!is_user_logged_in() && !is_page($public_pages) && ! (is_front_page() || is_home()) ){
wp_redirect(site_url());
exit();
}
}

My use: On a recent project we had a few pages that were ready enough, but a large custom post type of reviews which were not. It was hard for the client to remember not to hit publish and expose everything, but they needed to create a few posts so we could make sensible decisions on the design layout.

More generally is a good way to release early a few pages as they are ready enough.  There is more flexibility with this if you can conditionally assign alternative navigation in headers.

Bonus: CSS snippets to hide headers and footers for logged out users

These may not work as they are, but they make use of the fact that WordPress adds a .logged-in class selector to the backend.

/* A clumsy use of !important, but it is a temporary thing  */

header{display: none;}
.logged-in header{display: block!important;}

footer{display: none;}
.logged-in footer{display: block!important;}
/* slick but may not work */

header, footer:not(.logged-in){display: none;}
/* Wrong, but works (will show a warning in WPCodeBox's CSS editor, but not with SCSS) */

header:not(.logged-in header),footer:not(.logged-in footer){display: none;}

 

Do we  still need a coming soon plugin?

Perhaps?  There might be circumstances  where we need a different template to show on the root directory to the one being worked on. This is available with some page builders already. It’s not for Beaver Builder, so I will follow up with other options over at Beaver Junction. In short they are:

  • Make the homepage your “Coming soon” page and work on a Beaver Builder “home”  template then use one of the  above redirect to home snippets.
  • Use PowerPack (the addon pack for Beaver Builder). It has an excellent extension for this (with all the extra controls you might expect in a premium standalone plugin).
  • Use Beaver Themer’s conditionals to display different templates to logged in users. It’s convoluted,  but allows you to use its ability to display them according to referring URL, cookie or URL variable.

Credits:
The above php snippets could be attributed to others, but thanks to fellow Beaver Builder users: Neil Gowran and Grant Ambrose for getting me started.  A big shout out to Stranger Studios and, of course, to WPCodebox.

     Tag:

Leave a Comment