How to Create Custom 403 and 401 error pages in wordpress

Custom 403 and 401 error pages
Custom 403 and 401 error pages

A few days ago, one of our dear users sent me a question. How to create custom 403 and 401 error pages? I concluded that there is no way to create a 403 and 401 page 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 site template. Then write any code we want in this file and finally create 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 and the format of this page is also displayed from the server-side. like this:

403 error page
403 error page

In any case, there is always a way to do things in wordpress. Because wordpress has the ability to insert any type of 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 updating 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 to 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, that is, 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 just 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 file.

Good luck.

What do you think about this?

Please submit your comment only on the subject of this article. Your questions will be answered by the community.

Ask Question