There are a number of plugins available to allow you to create an Under Construction, Holding or Coming Soon page for your website. Most of these, however, rather limit the design that can be used on the page. Some only allow you to display an image, others give you a form to fill out where you specify information that is then displayed in some format or other, while some allow you to enter HTML. In this article, we are going to show you how to use an existing Divi page as the holding page. So you can design your Under Construction, Holding or Coming Soon page using the Divi Page Builder. Requests pages in the website will be automatically redirected to your holding page. We will however, arrange to allow access to pages if you are logged on to an account with admin privileges.

Let’s get started 🙂

How This Will Work

We are going to need to write some PHP to do this. The best way to add this PHP code to your website is to use a child theme and add it to your functions.php file. if you don’t have a child theme, there are lots of great articles available that detail how to create one (see Other Resources).

As it happens, there is another way of doing this without needing a child theme. We could simply save the code in a file (of file type PHP), zip it up and load it into our website as a plugin. If you want to try this, be sure to insert the following at the start of your file (do NOT do this if you are using the child theme approach) –

<?php
/*
Plugin Name: Holding Page
Plugin URI:
Description: Unless you are logged on as Admin, redirect all requests to the Holding Page if it is a published page 
Version: 1.0
Author: Two Blue Toucans
Author URI: www.twobluetoucans.co.uk
License:GPL
License URI:
*/

Our First Code

We need to redirect all page requests that WordPress receives to load our holding page instead. To do this we are going to use the WordPress action get_header (see Other Resources). In WordPress, actions and filters allow our code to run from within WordPress itself. In the case of the get_header action, our code will be run whenever WordPress processes page headers. So, after we do this, our code will be one of the first things that runs for every page request made to our website. This is exactly what we want.

To let WordPress know that we want our code to run whenever it processes page headers, we will use a WordPress function called add_action (see Other Resources). Here is what we need –

function redirect_to_holding(){
}

add_action( 'get_header', 'redirect_to_holding');

So, this adds our function redirect_to_holding to the get_header action. Our function, redirect_to_holding, though it does nothing at the moment (we will add code below), will now be run whenever WordPress processes page headers.

Getting Hold Of Our Holding Page

The first thing our code needs to do is check that our holding page exists. We will assume the holding page is called ‘Holding Page’. So, the actual URL path will end with ‘holding-page’. You should be able to confirm this by editing your holding page in the back end of Divi and just checking the permalink (beneath the page title). So, here is the next version of our code –

function redirect_to_holding(){

    $holding_page = get_page_by_path('holding-page'); 

    if ($holding_page != NULL) 
    {
        /* We have a holding page */
    }
}

add_action( 'get_header', 'redirect_to_holding');

So, we check to see if our holding page exists by using the WordPress function get_page_by_path. The result of calling this function is stored in the variable $holding_page. If our page does not exist, the function returns NULL and the following if statement fails. Our function would then do nothing. We now need to add the code inside the if statement, to redirect the visitor to the holding page.

Enabling and Disabling The Holding Page

It would be fantastic if we had a simple mechanism to allow us to enable and disable the holding page. It turns out that we can use its status to do this. If it exists but is in draft, we assume the holding page is disabled. If it exists and is published, we assume the holding page is enabled. We need to add a little extra to test for the page status –

function redirect_to_holding(){

    $holding_page = get_page_by_path('holding-page'); 

    if (($holding_page != NULL) && ($holding_page->post_status == 'publish')) 
    {
        /* We have a published holding page */
    }
}

add_action( 'get_header', 'redirect_to_holding');

We have added an extra clause to the if statement. It uses the post_status to check to see if the page is published or not. Now, the if statement will only succeed if the page both exists and is published.

So, we can use the status of the holding page as a switch to enable or disable it. This is convenient if we want to leave the holding page in the website. It will be its publication status that enables or disables the system, not whether the holding page exists or not.

Avoiding An Infinite Loop

Before we look at how we are going to display the holding page though, there is one special case we need to take care of. We need to make sure that our code does not attempt to redirect visitors to the holding page when WordPress itself is trying to display the holding page. If we don’t check for this situation, we will create an infinite loop – repeatedly redirecting ourselves to the holding page when WordPress is trying to display the holding page. Here is how we can avoid this –

function redirect_to_holding(){

    $holding_page = get_page_by_path('holding-page'); 

    if (($holding_page != NULL) && ($holding_page->post_status == 'publish'))
    {
         /* We have a published holding page */
        $current_page = get_post();
		
        if ($holding_page != $current_page) 
        {  	
            /* not trying to display the holding page so ok to redirect to holding page */
        }
    }
}

add_action( 'get_header', 'redirect_to_holding');

So, very simply, what we have done here is added an extra test. If the page WordPress is trying to display (the $current_page) is the holding page, simply drop through to the bottom of the function and exit without trying to redirect to the holding page. WordPress itself will display the holding page in this situation. 

Allow Admin To Access All Pages

It would be really convenient if, when we are logged on as an admin, we could access the website pages. All the other visitors would still be redirected to our holding page, even when we were logged on. To achieve this, we need to add another test to the code –

function redirect_to_holding(){

    $holding_page = get_page_by_path('holding-page'); 

    if (($holding_page != NULL) && ($holding_page->post_status == 'publish')) 
    {
         /* We have a holding page */
        $current_page = get_post();
		
        if ($holding_page != $current_page) 
        {  	
            /* not trying to display the holding page so ok to redirect to holding page */

            if (current_user_can('delete_users') == false) 
            {
                /* we are not logged on to an admin account */                            
            }
        }
    }
}

add_action( 'get_header', 'redirect_to_holding');

What this additional code does is to use current_user_can to see if the current user is logged on to an account that is able to delete other users (see Other Resources). We assume that, if this is true, they have admin privileges. Though you can change the capabilities associated with different roles, the default for admin accounts would grant this capability. So, only continue to the redirection if we cannot delete other users – we are (probably) not admin.

Lastly, Redirect To The Holding Page

Our code will do nothing if the holding page does not exist, or if it exists but is not published, or if the holding page is the page being displayed by WordPress, or if we are logged on to an account with administrator privileges. Otherwise, we want to redirect the visitor to the holding page instead of the requested page. To do this, we are going to use wp_redirect and get_permalink (see Other Resources). Here is the code –

function redirect_to_holding(){

    $holding_page = get_page_by_path('holding-page'); 

    if (($holding_page != NULL) && ($holding_page->post_status == 'publish')) 
    {
        /* We have a holding page */
        $current_page = get_post();
		
        if ($holding_page != $current_page) 
        {  	
            /* not trying to display the holding page so ok to redirect to holding page */

            if (current_user_can('delete_users') == false) 
            {
                /* we are not logged on to an admin account */
                wp_redirect(get_permalink($holding_page));
                exit;               
            }
        }
    }
}

add_action( 'get_header', 'redirect_to_holding');

So, wp_redirect does the redirection. It needs a full URL for the page and we use get_permalink to provide it for us. Notice that it is good practice to include an exit after calling wp_redirect as it does actually return control to the calling function. In our case, it is also a good idea to exit because we don’t want control to pass through the remainder of our procedure. Without the exit, any code after it might be executed. We don’t want that. We want to redirect the user to the holding page, nothing else. 

About The Holding Page

Just note that it is probably a good idea to create a holding page using the ‘Blank Page’ rather than the ‘Default Template’. You can set the template in the back end on the right in a panel titled Page Attributes. The blank page template does not include a header, footer or any sidebars.

If you found our blog article useful, why not subscribe? Get all of our articles and tutorials delivered direct to your inbox!

You have Successfully Subscribed!