Blog

Smart 404 for WordPress

Save visitors to your WordPress site from unhelpful 404 errors!

When a page cannot be found, Smart 404 will use the current URL to attempt to find a matching page, and redirect to it automatically. Smart 404 also supplies template tags which provide a list of suggestions, for use on a 404.php template page if a matching post can’t be immediately discovered.

Instead of quickly giving up when a visitor reaches a page that doesn’t exist, make an effort to guess what they were after in the first place. This plugin will perform a search of your posts, tags and categories, using keywords from the requested URL. If there’s a match, redirect to that page instead of showing the error. If there’s more than one match, the 404 template can use some template tags to provide a list of suggestions to the visitor.

This plugin is also useful if you have recently changed your permalink structure: With minimal or no adjustment, old permalinks will still work.

Download

Get Smart 404 over at the Smart 404 WordPress Plugin page!

Installation

  1. Unzip the package, and upload smart404 to the /wp-content/plugins/ directory on your WordPress site.
  2. Activate the plugin through the ‘Plugins’ menu in WordPress.
  3. Optionally, alter your theme’s 404.php template to list suggestions from Smart 404

Note: If you desire reporting on 404 errors that Smart 404 is unable to remedy, I recommend Joe Hoyle’s JH 404 Logger, which adds an item to your dashboard listing 404 errors. 404 Notifier by Alex King will send emails for 404 errors, but I hear reports that emails are sent for 404 errors that this plugin is able to recover from, not just unrecoverable errors.

Configuration

There are two configuration options for Smart 404:

Search

Turn on or off searching of posts, pages, tags and categories

Ignored patterns

A newline-separated list of terms or patterns to ignore from the URL. This is particularly useful for supporting old permalinks with an ID number in them. For example, to work with URLs like:

123-post-title.html

Add the regular expression pattern:

^[0-9]+-

This will ignore all numbers, followed by a hyphen, at the start of the URL.

Template Configuration

To provide a helpful list of suggested posts in your 404 pages, modify the 404.php template in your theme to use a Smart 404 template tag. For example:

<?php if (smart404_has_suggestions()) : ?>
Try one of these links:
<?php smart404_suggestions(); ?>
<?php endif; ?>

Or, for something a little more complicated:

<?php if (smart404_loop()) : ?>
<p>Or, try one of these posts:</p>
<?php while (have_posts()) : the_post(); ?>
<h4><a href="<?php the_permalink() ?>"
  rel="bookmark"
title="<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h4>
  <small><?php the_excerpt(); ?></small>
<?php endwhile; ?>
<?php endif; ?>

Note that smart404_loop() will only work for posts, not pages, due to limitations in the loop mechanism. Several template tags are supplied by Smart 404 for use in the 404.php template:

smart404_has_suggestions

Returns true if there are some suggestions, false otherwise

smart404_get_suggestions

Retrieve an array of post objects for rendering manually.

smart404_suggestions

Draw a list of suggested posts.

Pass the parameter “list” to render suggestions as a list.

smart404_loop

Query posts for use in a Loop. See the second example above for usage. Note that smart404_loop() will only work for posts, not pages, due to limitations in the loop mechanism.

Related posts

Tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

94 Comments

  1. BAC
    Posted June 28, 2010 at 4:00 pm | Permalink

    ah, thought it was me being a tool!

    Having played with the plugin a little more, for future releases maybe you would consider outputting the suggested pages as a list rather than divs as it’s easier to format

    Also adding relevancy to results would be cool. For some reason the most relevant page /post always seems to be the last suggestion, could be just fluke. I shall test more.

    Cheers BAC

  2. Posted June 29, 2010 at 4:20 pm | Permalink

    tks you for your so good plugin

  3. Posted July 18, 2010 at 5:06 pm | Permalink

    Thank you for a great plugin. However I had some problems when I installed it on my blog. When misspelling the url to a Page it didn’t work and ended up in endless redirect until firefox stopped it. I had a quick look in the code and found the error

    On line 100 it says wp_redirect( get_page_uri( $pages[0], 301 ) . $get_params );, I changed it to wp_redirect( get_permalink( $pages[0]->ID) . $get_params, 301 ); and now it works.

    And secondly if a page and a post have the same words the post will always be prioritized and the user redirected. I would like it to be the other way around. Or maybe even show the 404-screen for the user and let them pick between the page or the post. It would have been nice with an option in the administration menu to solve this?

    • Posted July 18, 2010 at 5:12 pm | Permalink

      tks you because i had disable the plugin for the same problem as you. but i hope this work and stay as that for the next version, i am going to try your form

    • Posted July 18, 2010 at 5:20 pm | Permalink

      follow : fatal error

    • Posted July 18, 2010 at 7:59 pm | Permalink

      Thanks for the catch, Emil!

      I’ve just released a fix, and also added the new feature of being able to prioritise, as you say. Let me know what you think.

      Cheers, Michael

  4. Posted July 18, 2010 at 8:31 pm | Permalink

    Thanks for the quick response. I just downloaded the new version and the reordering is really nice. Unfortunately there is still a bug when redirecting pages. In the same function the “)” is in the wrong place wp_redirect( get_page_uri( $pages[0] . $get_params, 301 ) ); should be wp_redirect( get_page_uri( $pages[0]) . $get_params, 301 );. But when I try it, I can’t get the get_page_uri() to work either, but when I change it to get_permalink it works fine. Here is my version of line 104: wp_redirect( get_permalink( $pages[0] ) . $get_params, 301 );

    If you have a public repository anywhere, like github and want some help maintaining the plugin I would love to contribute. Keep up the good work.

    • Posted July 18, 2010 at 9:17 pm | Permalink

      Damn! I hate it when that happens! I had some issues with symlinks and versioning and that slipped through. Been a long day =)

      Thanks again.

      I’d be very happy for you to contribute! I’ve emailed you

  5. Posted July 18, 2010 at 9:56 pm | Permalink

    Great tool! I love it and works well at 3.5 with WP3.0 However, on 4.0 and 4.0.1, I get a “Array()” in text at the top of the page… so downgraded again. Keep up the great work! Alban

    • Posted July 19, 2010 at 9:45 am | Permalink

      That is fixed in 4.0.2. It is working perfectly now :)

      • Posted July 19, 2010 at 9:47 am | Permalink

        Phew. Thank god – I shouldn’t be allowed outgoing network communications when I have days that my brain doesn’t work properly ;-)

  6. Paul
    Posted July 22, 2010 at 4:20 pm | Permalink

    Hello,

    I just started using your plugin and I like the functionality, however I was wondering if you could add a configuration option that stopped the automatic redirect. For my personal taste, I would rather the user get taken to my 404 page and be given the list of suggestions, rather than the auto redirect.

    Just a thought. Thanks Paul

    • Posted July 22, 2010 at 7:49 pm | Permalink

      Hi Paul,

      I’ll certainly consider it – for now, though, you can open up smart-404.php and comment out (prepend with “//”) all instances of “wp_redirect ( ...” and the immediately following “exit();” lines.

  7. Paul
    Posted July 22, 2010 at 6:16 pm | Permalink

    I had a second idea.

    You could use much of your plug-in’s functionality to help out on the search result page as well.

    Some plug-ins give spelling suggestions, but that can lead to suggesting words that aren’t even used in the blog…your plug-in could return suggested posts/pages. “similar posts” if there were some results and “suggested posts” if there were no results

  8. Posted July 23, 2010 at 2:27 pm | Permalink

    Sounds useful, will need to try this the next time I’m updating my site. Thanks in advance! :)

2 Trackbacks

  1. [...] Плагин позволяет вместо того чтобы показать пользователю обычную страницу об ошибке – попататься угадать что же он хотел увидеть на этой странице. Пользователь увидит результаты поиска ваших постов, тэгов и категорий используя ключевые слова из URL с которого он попал на эту страницу ошибки. Если есть совпадение, то пользователь сразу пересылается на целевую страницу без показа ошибки 404.  Если совпадений больше чем одного, то 404 шаблон может использовать несколько тэгов для показа различных  ссылок пользователю. Если вам это интересно, то пожалуйте на страницу автора плагина. [...]

  2. [...] Smart 404 for WordPress – плагин воспользуется поиском и выдаст пользователю список страниц, подходящих под набранный им урл, вместо 404-ой ошибки. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Subscribe without commenting