Custom 404 for WordPress

By admin Jul 12, 2016 in SEO


Give misguided visitors a softer landing

Successful blogs by their very nature tend to grow and evolve exponentially over time. Adding new categories and topics to your blog structure doesn’t pose much of an issue but, What do you do when you need to change the structure and categorical markup of your blog but don’t want folks/search engines getting whacked with “PAGE NOT FOUND” errors?

Giveem options

By default, many contemporary WordPress themes come with a rather boilerplate 404 page. The basic “Sorry but…” template with a hook to display the vanilla search form doesn’t really help folks coming in from a search portal, looking for something specific.

With a small bit of code snipped from the wordpress codex, a great plugin from the raproject and a copy of your default 404.php you can help viewers find what they were looking for (and then some).

Get yer goodies

Step 1.- Get the code

Head over to wordpress codex , and copy the example code found under “Sending proper headers” then, scroll down to just below “Tips for Error pages” and highlight/copy the complete code example. Now, paste both these in your favorite text-editor.

Step 2.- Get the plugin

  • Visit raproject.com (readers appreciation project) and download the plugin WP Categories and Posts v1.0.
  • Uncompress the zip and FTP it to your /wp-content/plugins/ directory.
  • Bop over to your WP site, go to your plugins and “activate” the plugin.

Step 3.- Edit the template

While in your WP admin, go to Presentation, click on Theme Editor and under the “Theme Files” column select/click 404 template.

If your theme didn’t come with a 404.php, you can try making a copy of your basic theme (index.php) renaming it 404.php and modifying it to exclude anything dealing with sections and comments..

Step 4.-Add code snippets

An issue with wordpress is, by default.. even IF a page or post isn’t found, it still returns a code 200 after a page is requested (meaning the page requested is fine) which, obviously it’s not.

That’s not playing nice with the search engines and spiders so, we’ll want to be truthful and own up to the broken link. That’s where the first snippet of code comes in and, is pasted on the very first line of your new 404.php template, before the get_header().

The next snippet basically sniffs out and displays the bad URL (the webpage folks THOUGHT they were coming to read) and helps folk recover from an improperly typed in address (I’ve done it…) and proves they’re not totally lost in the ether.

You can place the second snippet pretty much anywhere you want on the 404.php page, modify the default “sorry…” to reflect your sensibilities.

Step 5.-Add categories and Post links

The final bit of code I’ll go over is how to add the function that calls and displays all your blog categories and posts so lost 404 souls can easily find either the lost/renamed article they came looking for or, select/view another, relevant post.

Find the area on your 404.php page where you want to display your categories and posts lists and add the following:

Done almost..

In a nutshell what we’ve just done is;

  • Told search engines and spiders, sorry.. we goofed but here’s tons more relevant stuff for ya.
  • Display to folks the url they were following - so they don’t get mad or, think they’re goin nuts.
  • Sent an email back to you, listing the offending URI so you can fix it.
  • Given lost souls a plethora of link/cat/post options to keep em on your site.
  • Given ourselves a “warm-fuzzy” knowing we’re making the web a better place.

Now all that’s left up to you is to add your glorious style/css to make your 404 unique.

Leave Yours +


Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *