WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When a 404 error is encountered, a 404.php template file can be used to render a custom error message. This is documented in the Template Hierarchy article.

My aim is to create custom error pages for 401, 403 and 500 errors. I've tried creating 401.php, 403.php and 500.php files in my theme's directory but this doesn't work.

How can I create custom 401, 403 and 500 error pages in WordPress?

share|improve this question
You could try using this code as a starting point: jesin.tk/wordpress-custom-403-401-error-page – Joe May 1 '14 at 11:48
Here's an existing plugin that lets you customize error pages like 403 and 401 errors: http://wordpress.org/plugins/custom-error-pages/ All you have to do is install and activate. However, you have to manually edit the .htaccess (or nginx.conf) file specified in the plugins options page. – Pi Lover May 1 '14 at 13:56
up vote 6 down vote accepted

Error pages are served up via .HTACCESS, if you are using Apache you would use the ErrorDocument directive and add the status and URL to it.

So it would look like this in your .htaccess file:

ErrorDocument 401 http://yourwebsite.com/error-401
ErrorDocument 403 http://yourwebsite.com/error-403
ErrorDocument 500 http://yourwebsite.com/error-500

You could use the following function below. This will dynamically add what is needed to the HTACCESS file for you or you could manually do it.

1. Add Pages:

You would then need to go into your Dashboard and create the Pages like any normal page ('Dashboard' > 'Pages' > 'New'). They can be titled what ever you wish, just make sure the slug is the same as in the function below (Slug: error-401, error-403, error-404, error-500). Also you can use page template to create whatever layout and style you want for these specific pages. Follow WordPress Codex instructions for this.

2. Add Function:

// Create Custom Error Pages in WordPress using HTACCESS
function royal_custom_error_pages() {

    // Get HTACCESS path & dynamic website url
    $htaccess_file = '.htaccess';
    $website_url = get_bloginfo('url').'/';

    // Check & prevent writing error pages more than once
    $check_file = file_get_contents($htaccess_file);
    $this_string = '# BEGIN WordPress Error Pages';

    if( strpos( $check_file, $this_string ) === false) {

    // Setup Error page locations dynamically
    $error_pages .= PHP_EOL. PHP_EOL . '# BEGIN WordPress Error Pages'. PHP_EOL. PHP_EOL;
    $error_pages .= 'ErrorDocument 401 '.$website_url.'error-401'.PHP_EOL;
    $error_pages .= 'ErrorDocument 403 '.$website_url.'error-403'.PHP_EOL;
    $error_pages .= 'ErrorDocument 404 '.$website_url.'error-404'.PHP_EOL;
    $error_pages .= 'ErrorDocument 500 '.$website_url.'error-500'.PHP_EOL;
    $error_pages .= PHP_EOL. '# END WordPress Error Pages'. PHP_EOL;

    // Write the error page locations to HTACCESS
    $htaccess = fopen( $htaccess_file, 'a+');
    fwrite( $htaccess, $error_pages );


add_action('init','royal_custom_error_pages'); // This will run the function everytime, not ideal!

// register_activation_hook( __FILE__, 'royal_custom_error_pages' ); // Using a plugin, runs only once!


When moving your website or changing URL structure

The thing to remember with the above function is although it will check to see if the ErrorDocument directives already exist before writing them to your HTACCESS file it will not rewrite the ErrorDocument directives should you change or move your blog to reflect the updated page locations. You would need to delete the existing ErrorDocument directives in your HTACCESS file first and then rerun this function to create the new directives.

Correct Hook to fire the function ONLY ONCE

The other thing to note is this function, using the init action will run every time your pages load which is super unnecessary and wasteful so I would suggest adding it to a plugin and using the register_activation_hook rather so it only fires once on plugin activation

File Permissions

Also it is imperative that your .htaccess is writable when using the above function, so make sure it has the correct file permissions, something like CHMOD777.

share|improve this answer
thanks for these ideas. I think the manual modification to the .htaccess file is my favourite approach in the absence of a native way of doing it in WordPress. – henrywright Aug 31 '14 at 10:44
Anytime, glad it was of interest to you :-) – Matt Royal Sep 1 '14 at 11:43
Not sure how this is suppose to work, did the edits manually to the .htaccess file, but this does not work. I am trying to do this on localhost and this should be possible on localhost, since I have access to everything, the server, and client. But why is this not working? – Solomon Closson Apr 26 at 3:34
@SolomonClosson, perhaps you can try using the plugin I wrote for this, it will do everything for you and follows the code above exactly :). Official WordPress.Org Plugin – Matt Royal Apr 26 at 8:04

Adding unnecessary plugins to WordPress is risky and if you have a self-hosted blog with Cpanel, you can create custom error pages very simply.

Find Advanced and click on Error Pages.

All of the errors are listed, so click the one you want to edit and input your content.

Click Save and go to the next one.

There are lots of videos on YouTube showing details.

I have done this with 404 pages especially and it is really easy.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.