The old WordPress 404 wasn’t any help. I used a fantastic plugin, Smart404, that no longer exists. Thew new one, for whatever reason, shows recent pages, with no regards to relevance or pages. How useless! What’s a girl woman to do? She spruces up her own 404, of course.
The first thing I wanted to do was to show the URL that the visitor intended to go to. To test, I had to come up with a URL that wouldn’t redirect. WordPress will try to match up broken URLs to existing page and post titles, which already takes care a lot of broken pages. A complex URL such as “http://7and1.net/isnt-this-funny” shows my 404. WordPress tags themselves won’t show you the intended address, because WordPress registers you are being on the 404 page, so I had to use a little basic PHP to show the URL:
php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?
I inserted this inside some friendly text, so my 404s say “It looks like you were looking for [URL].
Next, I wanted to suggest related pages. My now-defunct plug-in was so good at this, and I spend a couple hours looking for a substitute that would essentially turn the URL of the broken link into search keywords. Nothing exists. If you make this plugin, email me and I will reward you well!
Instead, I opted for the next best thing: related posts. The problem is, most related posts plugins look at only the page content, tags or categories, none of which exist for a broken link. So, I decided to simply do some trial and error, to see which plugins would get me the closest and work on the 404 page. I wound up replacing my existing related posts plugin for the blog, too, because who needs more than one? I settled on nRelate, because it seems to do the best job comparing URLs to keyword in posts that actually exist.
I inserted the code into my 404.php theme file like this:
php if (function_exists('nrelate_related')) nrelate_related(); ?
But I wasn’t done. The last thing that I wanted to enable my visitors to do was to contact me, which was really only duplicating the report an error page that I already have. It’s kind of out of the way, so I figured that some users might be more likely to use it on the actual 404. However, I expect few people to do this. As a webmaster, we have ways to check errors and broken links, this just provides me with a little more information.
I use Contact Form 7, which has both PHP and an in-post shortcode. Because I was editing my 404 page, I needed to use the former, so I added this last chunk to my 404 page.
php echo do_shortcode( '[contact-form]' ); ?
The shortcode actually breaks it in this tutorial. Oops, but you can see where I went.
So, now my 404s show the visitor the name of the link and some possibly related entries. If none of those are good, they can at least see the link to use the search bar, which appears on every page. If this is all my fault, because I’m a horrible person, my visitors can let me know via the form. In my opinion, this tutorial makes a much more complete 404 than any default WordPress has ever used.
Psst, I have more tutorials here.
The post Spicing Up the WordPress 404 appeared first on The Scrolls.