Skip to content

Instantly share code, notes, and snippets.

@stephanieleary
Last active May 11, 2016 16:03
Show Gist options
  • Save stephanieleary/06bd439a6a5b834bf02cec35208d0b39 to your computer and use it in GitHub Desktop.
Save stephanieleary/06bd439a6a5b834bf02cec35208d0b39 to your computer and use it in GitHub Desktop.
Redirect 404 errors to posts with old path stored by HTML Import
<?php
add_filter( 'redirect_canonical', 'my_404_no_guessing', 10, 2 );
function my_404_no_guessing( $redirect_url, $request_url ) {
if ( is_404() ) {
// HTML Import redirects
$posts = get_posts( array(
'meta_key' => 'URL_before_HTML_Import',
'meta_value' => $request_url,
'post_type' => 'any',
'posts_per_page' => 1,
'fields' => 'ids',
) );
if ( $posts ) {
$redirect_url = get_permalink( array_pop( $posts ) );
}
else
return false;
}
return $redirect_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment