Creating Helpful 404 Pages

While a site owner usually tries to do everything imaginable to create a site without errors, the occasional 404 error will crop up from time to time. Instead of just telling the visitor the page was an error, and increasing the bounce rate of the site, give the visitor other reading options. This is better for the visitor and the site owner. The visitor will be more likely to stay around if the links are to similar topic to the one they were looking for. This is were helpful 404 pages come into play.

The code itself is fairly simple and straight forward. What it does is take the end of the url after the final slash, and strips any extensions like .html, or .php that might have been accidentally added. It then queries for a post or page or any custom post type that has a title with this string. If one or more is found it will output it in a list. If this comes up empty then it will replace any dashes or underscores with spaces and use this as a search string. This will return a list of entries that contain these words. Using these two ways some results should be found and give the visitor something else that should be closely related to what they were looking for.

Idea originally obtained from Yoast.

About Jeremy Clark

Small town IT worker with interests in all things technological and technical. Biggest interests are in web development especially the WordPress publishing platform and the community supporting it. Currently developing and maintain the free WordPress theme Techozoic. I'm also always available for hire.
This entry was posted in Development, Featured, wordpress. Bookmark the permalink.

Leave a reply

Comments will be sent to the moderation queue.