In my last tutorial I showed you how to remove paginated page URLs in WordPress. In that guide I shared code to 301 redirect paginated URLs that aren’t needed. However, in some cases using a 301 redirect isn’t appropriate & returning a 404 status is best. In this tutorial I will show you how to force return a 404 error page in WordPress using a little code.

Important Note: This guide is intended to show you how to return a 404 error page in WordPress using PHP code and WP hooks. In an ideal setting you would do this server-side to prevent loading WordPress twice. The code shared in this guide is for those that can’t add custom server rules or for solutions that require conditional WordPress checks.

Skip to the code snippet

What is a 404 Error Page?

A 404 error page is used to let your site visitor know that the requested page does not exist. This is the correct response to return if a page has never existed on your website. WordPress will automatically display a 404 error page whenever someone visits a non-existing URL on your site.

However, there are cases where WordPress may still display a page even if it doesn’t exist. There are few cases, but one example is paginated URLs. On most WordPress sites, even if they are using a static homepage you may be able to access the paginated pages such as site.com/page/2/. This is because WordPress adds rewrite rules for all pages to allow pagination.

There may be situations where WordPress is “creating” a page that doesn’t exist or you don’t want it to exist. That’s where this tutorial comes in handy. I will show you how to force return a 404 error page on your WordPress site.

And of course, you can remove WordPress rewrite rules to prevent unwanted URLs, but this isn’t always possible. You can also redirect pages using 301 or 302 redirect codes, but this isn’t always best for SEO.

Using Code to Force a 404 Error Status Page in WordPress

To force return a 404 error page in WordPress is very simple. All you have to do is use the public $wp_query->set_404() method.

There are multiple hooks you can use to “attach” your code and run the mentioned function. Personally, I recommend using wp hook. By using the wp hook you are able to use conditional tags and ensure you are targeting the correct pages.

Have a look at some sample snippets below:

Forcing a 404 Error Page for a Specific WordPress Page

Here is a simple snippet for returning a custom 404 error on a specific page:

/**
 * Return a 404 error page for a specific page in WordPress.
 *
 * @link https://www.wpexplorer.com/force-404-error-page-wordpress/
 */
function wpexplorer_force_404_error() {
	if ( is_page( 'contact' ) ) {
		global $wp_query;
		$wp_query->set_404();
		status_header( 404 );
	}
}
add_action( 'wp', 'wpexplorer_force_404_error' );

If you add this code to your site and someone tries to visit yoursite.com/contact/ they will be shown a 404 page. Be sure to modify the is_page() conditional check to suit your needs. You can of course use any checks you want.

Forcing a 404 Error Page for a Specific WordPress Category

If you want to return a 404 error page for a specific category archive it’s very simple. Below is a modified snippet showing you how it’s done.

/**
 * Return a 404 error page for a specific category in WordPress.
 *
 * @link https://www.wpexplorer.com/force-404-error-page-wordpress/
 */
function wpexplorer_force_404_error() {
	if ( is_category( 'featured' ) ) {
		global $wp_query;
		$wp_query->set_404();
		status_header( 404 );
	}
}
add_action( 'wp', 'wpexplorer_force_404_error' );

The former code snippet would be useful if you have a category that you don’t want an archive for. For example, if you have a featured category that you are using only to showcase specific posts on your homepage. For SEO reasons, you wouldn’t want your “featured” category indexed and thus returning a 404 error page is best.

Prevent Browser Caching of the 404 Error Page

Whenever a website returns a 404 error page, the browser may cache the response (it seems like Chrome does). For most cases this is completely fine, however, if you want to temporarily display a 404 page you should stop browser caching.

To prevent the browser from caching the 404 status code you can use the core WordPress nocache_headers function. From my experience, this function isn’t super reliable but it does seem to work ok in most modern browsers.

Simply add the function after the status_header function like such:

global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers(); // ADD THIS!

With the added code, if a user visits the page and it shows a 404 error & then you make the page live, when they come back they will be able to see it.

Returning a 404 Page in WordPress is Easy!

As you can see it’s very simple to return a 404 error page in WordPress whenever you need to. I’m personally using code on WPExplorer.com to return a 404 page if anyone tries going to a paginated page on our homepage such as wpexplorer.com/page/2/.

Let me know in the comments if you have any issues, questions or if you want to share how you are making use of the code snippet.

Similar Posts