How to Force 404 Error Page in WordPress

- Advertisement -

How to Force Return a 404 Error Page in WordPress

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, and 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.

- Advertisement -

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.

What is a 404 Error Page?

- Advertisement -

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.

- Advertisement -

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 the 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.

The post How to Force Return a 404 Error Page in WordPress appeared first on WPExplorer.

- Advertisement -

Related articles

Product Marketing Strategy: A Step-by-Step Guide

How to Create a Product Marketing Strategy: A Step-by-Step Guide

Learn to craft a product marketing strategy with our detailed guide and effectively reach your target audience.

Measuring Influencer Marketing Success & ROI: A Guide

How to Measure Influencer Marketing Success & Return on Investment

What’s the best way to measure your next influencer marketing campaign? Find out with this article.

Fixing Gutenberg Fonts in Classic WordPress Themes

If you are using a Classic WordPress theme it’s possible that the Gutenberg editor looks like sh*t because it’s using default tiny serif fonts that can be difficult to read and write with. This simple guide will show you how you can fix this by customizing the default Gutenberg typography! Method 1: Using Inline CSS […]

The post Fix Gutenberg Fonts in Classic WordPress Themes appeared first on WPExplorer.

Finding Micro Influencers for Your Brand

How to Find Micro Influencers For Your Brand

With a small audience, micro influencers can have a big impact on your marketing strategy. Here‘s how to find them.

WordPress: Remove or Change “Howdy”

WordPress uses the phrase “Howdy!” in various locations of the admin including the toolbar at the top where it reads “Howdy! {User}”. If you find this to be a little unprofessional (like I do) and want to change or remove it, you’ve come to the right place. I will show you how. How to Remove […]

The post How to Remove or Change “Howdy” in WordPress appeared first on WPExplorer.