[Patch] Smart404, sane post redirection behavior & sticky posts
-
This patch tries to improve the abandoned Smart404 plugin. The main change is to not redirect if a post or page matches the request unless only one post or page matches. This is different from the default behavior where if only one post matches but many pages match, the post will still be redirected to. Other tweaks are to remove unnecessary exit commands, and to try to to exclude sticky posts from the recommendation list (although this seems to fail in WP3.5).
--- /tmp/smart-404/smart404.php 2010-09-14 14:22:33.000000000 -0500 +++ smart404.php 2012-12-20 09:53:26.000000000 -0600 @@ -3,7 +3,7 @@ Plugin Name: Smart 404 Plugin URI: https://atastypixel.com/blog/wordpress/plugins/smart-404/ Description: Rescue your viewers from site errors! When content cannot be found, Smart 404 will use the current URL to attempt to find matching content, 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 matching content can't be immediately discovered. -Version: 0.5 +Version: 0.5 PATCHED: don't redirect when one page matches if multiple posts have also matched, try to avoid sticky posts in recommendations Author: Michael Tyson Author URI: https://atastypixel.com/blog/ */ @@ -75,10 +75,9 @@ switch ( $group ) { case "posts": // Search for posts with exact name, redirect if one found - $posts = get_posts( array( "name" => $search, "post_type" => "post" ) ); + $posts = get_posts( array( "name" => $search, "post_type" => "post" ) ); if ( count( $posts ) == 1 ) { wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 ); - exit(); } break; @@ -87,7 +86,6 @@ $posts = get_posts( array( "name" => $search, "post_type" => "page" ) ); if ( count( $posts ) == 1 ) { wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 ); - exit(); } break; @@ -96,7 +94,6 @@ $tags = get_tags( array ( "name__like" => $search ) ); if ( count($tags) == 1) { wp_redirect(get_tag_link($tags[0]->term_id) . $get_params, 301); - exit(); } break; @@ -105,7 +102,6 @@ $categories = get_categories( array ( "name__like" => $search ) ); if ( count($categories) == 1) { wp_redirect(get_category_link($categories[0]->term_id) . $get_params, 301); - exit(); } break; } @@ -113,28 +109,22 @@ // Now perform general search foreach ( $search_groups as $group ) { + #We could do away with this switch if the stored option was singular switch ( $group ) { case "posts": $posts = smart404_search($search, "post"); - if ( count( $posts ) == 1 ) { - wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 ); - exit(); - } - - $GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts ); + $GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts ); break; case "pages": $posts = smart404_search($search, "page"); - if ( count( $posts ) == 1 ) { - wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 ); - exit(); - } - - $GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts ); - break; + $GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts ); + break; } } + if( count($GLOBALS["__smart404"]["suggestions"]) == 1 ){ + wp_redirect( get_permalink( $GLOBALS["__smart404"]["suggestions"][0] ) . $get_params, 301 ); + } } @@ -149,7 +139,15 @@ */ function smart404_search($search, $type) { $search_words = trim(preg_replace( "@[_-]@", " ", $search)); - $posts = get_posts( array( "s" => $search_words, "post_type" => $type ) ); + $posts = get_posts( array( "s" => $search_words, + "post_type" => $type, + + #Alas, neither of these seem to work in 3.5 + 'ignore_sticky_posts' => true, +# 'post__not_in' => get_option('sticky_posts') + ) + ); + if ( count( $posts ) > 1 ) { // See if search phrase exists in title, and prioritise any single match $titlematches = array();
A work-around for the failure to exclude sticky posts is to do so in your 404 template e.g;
<?php if (smart404_loop()) : ?> <?php while (have_posts()) : the_post(); ?> <?php #Work-around get_posts ignoring post__not_in and ignore_sticky_posts if( ! is_sticky() ){ ?> <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; ?> </div> <?php } ?>
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘[Patch] Smart404, sane post redirection behavior & sticky posts’ is closed to new replies.