-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 404 page with redirects for legacy URLs
- Loading branch information
Joseph Mornin
committed
Jun 1, 2012
1 parent
117f9e1
commit 15b8b6c
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php get_header(); ?> | ||
|
||
<article id="entry"> | ||
<div class="row"> | ||
<div class="span8"> | ||
|
||
<?php | ||
|
||
// Redirect legacy urls, which have the structure of: | ||
// /blog/2009/01/title.html or | ||
// /news/2009/01/title.html or | ||
|
||
$uri = $_SERVER['REQUEST_URI']; | ||
|
||
$pos_blog = strpos($uri, 'blog/'); | ||
$pos_news = strpos($uri, 'news/'); | ||
|
||
if ($pos_blog || $pos_news) { | ||
$filename = explode('/', $uri); | ||
$filename = $filename[sizeof($filename) - 1]; | ||
$filename = substr($filename, 0, -5); | ||
$filename = str_replace("_", " ", $filename); | ||
$filename = ereg_replace("[^A-Za-z ]", "", $filename); | ||
?> | ||
<script type="text/javascript"> | ||
window.location = '<?php echo(site_url()); ?>/?s=<?php echo(urlencode($filename)); ?>'; | ||
</script> | ||
<br /> | ||
<p>You're being redirected to the page you requested. If you're not redirected within a few seconds, <a href="<?php echo(site_url()); ?>/?s=<?php echo(urlencode($filename)); ?>">click here</a> to continue.</p> | ||
<?php | ||
} else { | ||
?> | ||
<h2 class="entry-title">Not Found</h2> | ||
<p>Sorry, we couldn't find the page you requested.</p> | ||
<?php | ||
} | ||
?> | ||
</div><!--/.span8--> | ||
</div><!--/.row--> | ||
</article> | ||
|
||
|
||
<?php get_footer(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
// Redirect to post if search returns only one result | ||
function redirect_to_single_search_result() { | ||
if( is_search() ){ | ||
global $wp_query; | ||
if( $wp_query->post_count == 1 ){ | ||
wp_redirect( get_permalink($wp_query->posts['0']->ID), 302 ); | ||
exit; | ||
} | ||
} | ||
} | ||
add_action('template_redirect', 'redirect_to_single_search_result'); | ||
|
||
// Restrict search to post titles | ||
function __search_by_title_only( $search, &$wp_query ) { | ||
global $wpdb; | ||
|
||
if ( empty( $search ) ) | ||
return $search; // skip processing - no search term in query | ||
|
||
$q = $wp_query->query_vars; | ||
$n = ! empty( $q['exact'] ) ? '' : '%'; | ||
|
||
$search = | ||
$searchand = ''; | ||
|
||
foreach ( (array) $q['search_terms'] as $term ) { | ||
$term = esc_sql( like_escape( $term ) ); | ||
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')"; | ||
$searchand = ' AND '; | ||
} | ||
|
||
if ( ! empty( $search ) ) { | ||
$search = " AND ({$search}) "; | ||
if ( ! is_user_logged_in() ) | ||
$search .= " AND ($wpdb->posts.post_password = '') "; | ||
} | ||
|
||
return $search; | ||
} | ||
add_filter( 'posts_search', '__search_by_title_only', 500, 2 ); | ||
?> |