WordPress – Improve your 404 File Not Found answers (4 of 4)
In our previous post talking about improving your 404 File Not Found answers in WordPress we have talk about the great plugin Smart 404 to give your viewers more chances. It performs a search of your posts, tags and categories, using keywords from the requested URL and if there’s a match, Smart 404 redirects to that page instead of showing the 404 File Not Found error.At DiceLock Security Blog we prefer to show them the 404 File Not Found, and there is a way to do it…
| Applies to:
WordPress 2.8 |
These customizations are provided “AS IS” without warranty of any kind and DiceLock Security disclaims all warranties, EITHER EXPRESS OR IMPLIED INCLUDING THOSE OF MERCHANTABILITY, NONINFRINGEMENT OF THIRD-PARTY INTELLECTUAL PROPERTY, OR FITNESS FOR A PARTICULAR PURPOSE.
Upgrading or reinstalling the customized source code (package, plugin, component, …) may remove the customizations performed. |
In our previous posts we have shown how to add a Search box, a Site Map and recommended links in your 404 File Not Found error page, allowing your viewers to search what they are looking for, as in:
Smart 404 performs a search of your posts, tags and categories, using keywords from the requested URL. If there’s a match, redirects to that page instead of showing the error. At DiceLock Security Blog we believe that such performance could make our viewers to leave the site.
If a visitor has arrived to our web site for some specific reason that remains in our web site, but it’s not provided through the URL link used by the visitor, if we show him a correct page not related to the reason, maybe the viewer will get out from our web site. So, we prefer to show him that the link through which he has arrived does not exist and we show him the tools that will help him to arrive to the information he looks for.
What we have done is to comment some code lines in smart404.php file.
file: smart404.php
lines: 57-87
if ( in_array( 'posts', (array)get_option( 'also_search' ) ) ) { // Search for posts with exact name, redirect if one found $posts = get_posts( array( "name" => $search ) ); /** // if ( count( $posts ) == 1 ) { // wp_redirect( get_permalink( $posts[0]->ID ), 301 ); // exit(); // } */ // Do a general search, redirect if exactly one result $posts = get_posts( array( "s" => $search ) ); // if ( count( $posts ) == 1 ) { // wp_redirect( get_permalink( $posts[0]->ID ), 301 ); // exit(); // } else if ( count( $posts ) > 1 ) { $GLOBALS["__smart404"]["suggestions"] = $posts; // } } // Search for pages if ( in_array( 'pages', (array)get_option( 'also_search' ) ) ) { $pages = get_posts( array( "s" => $search, "post_type" => "page" ) ); // if ( !$GLOBALS["__smart404"]["suggestions"] && count( $pages ) == 1 ) { // wp_redirect( get_page_uri( $pages[0], 301 ) ); // exit(); // } else if ( count ($pages ) > 0 ) { $GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $pages ); // } }
With this modification your viewers will get something like the following screenshot when they get a 404 File Not Found error when there is a Smart 404 match page:
As we have commented in our previous posts, in this way, your viewers will get in front of their eyes tools that will make them to stay in your site.










































