How to Create Custom 403 and 401 Error Pages in WordPress

How to Create Custom 403 and 401 Error Pages in WordPress

One of our dear users sent me a question a few days ago. How to create custom 403 and 401 error pages? I concluded that there is no way to make 403 and 401 pages in WordPress unless we use other methods to build these custom pages.

To create a dedicated 404 error template in WordPress, we can create a 404.php file in our template. Then write any code we want in this file and finally make such an example for a 404 error:

Custom wordpress 404 page example
Custom WordPress 404 page example

But for error 403, this method is not responsible. Because error 403 is called from the server-side, this page’s format is also displayed from the server-side. like this:

403 error page
403 error page

However, there is always a way to do things in WordPress. Because WordPress can insert any PHP code. So let’s see how to create custom 403 and 401 pages in WordPress.

Create a Child-theme

First of all, if you have not created a child theme for your website template, start building a child theme right now. If you do not know what a child theme is, read our article about the child theme. This is why we create a child theme that if you use a WordPress template like the Ahura template, naturally, these templates will be updated, and by editing the template, your changes will be deleted.

Create Custom 403 and 401 error pages

Now we enter the template folder or the child theme. Then we create a file with the following name.

403.php

This file will save the contents of the error page 403. For example, in this file, we can write: Welcome, but you do not have access here!

Of course, there is a better way. Copy the 404.php file and save it as 403.php

We do the same for error 401; we create a file with the following name in the template folder and write our desired code there.

401.php

Edit functions.php

Now we come to the part where we talk about the middle ground. Just enter the functions.php file of your website template. Then add these codes to this file. Codes must be added at the end of the Functions file before ?>

function mihanwp_custom_error_pages()
{
    global $wp_query;
 
    if(isset($_REQUEST['status']) && $_REQUEST['status'] == 403)
    {
        $wp_query->is_404 = FALSE;
        $wp_query->is_page = TRUE;
        $wp_query->is_singular = TRUE;
        $wp_query->is_single = FALSE;
        $wp_query->is_home = FALSE;
        $wp_query->is_archive = FALSE;
        $wp_query->is_category = FALSE;
        add_filter('wp_title','mihanwp_custom_error_title',65000,2);
        add_filter('body_class','mihanwp_custom_error_class');
        status_header(403);
        get_template_part('403');
        exit;
    }
 
    if(isset($_REQUEST['status']) && $_REQUEST['status'] == 401)
    {
        $wp_query->is_404 = FALSE;
        $wp_query->is_page = TRUE;
        $wp_query->is_singular = TRUE;
        $wp_query->is_single = FALSE;
        $wp_query->is_home = FALSE;
        $wp_query->is_archive = FALSE;
        $wp_query->is_category = FALSE;
        add_filter('wp_title','mihanwp_custom_error_title',65000,2);
        add_filter('body_class','mihanwp_custom_error_class');
        status_header(401);
        get_template_part('401');
        exit;
    }
}
 
function mihanwp_custom_error_title($title='',$sep='')
{
    if(isset($_REQUEST['status']) && $_REQUEST['status'] == 403)
        return "Forbidden ".$sep." ".get_bloginfo('name');
 
    if(isset($_REQUEST['status']) && $_REQUEST['status'] == 401)
        return "Unauthorized ".$sep." ".get_bloginfo('name');
}
 
function mihanwp_custom_error_class($classes)
{
    if(isset($_REQUEST['status']) && $_REQUEST['status'] == 403)
    {
        $classes[]="error403";
        return $classes;
    }
 
    if(isset($_REQUEST['status']) && $_REQUEST['status'] == 401)
    {
        $classes[]="error401";
        return $classes;
    }
}
 
add_action('wp','mihanwp_custom_error_pages');

Just as easily! Now, look at one of the pages of your website that has a 403 error.

The error will be displayed as code stored in the 403 files.

Good luck.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

What do you think about this?