301 Redirect Code
I am currently trying to redirect a subdomain.blogger.com blog to a domain located on my hosted WordPress install. The code below is supposed to send a 301 redirect and then make that specific post appear on my blog but it appears to be directing the user only to the home page and not to the corresponding blog post.
/************** 301 REDIRECTION ***********************/
function labnol_blogger_query_vars_filter( $vars ) {
$vars[] = "blogger";
return $vars;
}
add_filter('query_vars', 'labnol_blogger_query_vars_filter');
function labnol_blogger_template_redirect() {
global $wp_query;
$blogger = $wp_query-query_vars['blogger'];
if ( isset ( $blogger ) ) {
wp_redirect( labnol_get_wordpress_url ( $blogger ) , 301 );
exit;
}
}
add_action( 'template_redirect', 'labnol_blogger_template_redirect' );
function labnol_get_wordpress_url($blogger_slug) {
global $wpdb;
if ( preg_match('@^(?:https?://)?([^/]+)(.*)@i', $blogger_slug, $matches) ) {
$q = "SELECT guid FROM $wpdb-posts LEFT JOIN $wpdb-postmeta
ON ($wpdb-posts.ID = $wpdb-postmeta.post_id)
WHERE $wpdb-postmeta.meta_key='blogger_permalink'
AND $wpdb-postmeta.meta_value='$matches[2]'";
$wp_url = $wpdb-get_var($q);
}
return $wp_url ? $wp_url : home_url();
}
/************** 301 REDIRECTION ***********************/
My Wordpress permalink structure is set to custom with the value:
/%year%/%monthnum%/%postname%.html
Any help would be greatly appreciated.