Visit us on FacebookVisit us on TwitterVisit us on YoutubeVisit us on SteamVisit us on TwitchCheck out our Feed

Forums

Welcome Guest 

Show/Hide Header

Welcome Guest, posting in this forum requires registration.





Pages: [1]
Author Topic: Wordpress 3.7.1 add custom error pages 401 403 404
Darwood37
Administrator
Posts: 12
Permalink
Post Wordpress 3.7.1 add custom error pages 401 403 404
on: November 25, 2013, 16:37
Quote

Add the following code to functions.php

function 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','custom_error_title',65000,2);
        add_filter('body_class','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','custom_error_title',65000,2);
        add_filter('body_class','custom_error_class');
        status_header(401);
        get_template_part('401');
        exit;
    }
}
 
function 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 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','custom_error_pages');

Create the 404.php (the default built in page for WP) and place it in your themes directory along with your custom 403.php and 401.php files.

Edit .htaccess to use custom error pages:

ErrorDocument 404 /wordpress/index.php?error=404
ErrorDocument 403 /wordpress/index.php?status=403
ErrorDocument 401 /wordpress/index.php?status=401

error 404 is built in to WP so it is called via error, the others are called by the new function status.

Pages: [1]

Looking for something?

Use the form below to search Gaveroid.com:

Still not finding what you're looking for?
Contact us so we can take care of it!