Created
September 12, 2017 12:09
-
-
Save aman5893/99d1aa41604a5fe091b66fc722cabad4 to your computer and use it in GitHub Desktop.
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
function blogger_query_vars_filter( $vars ) { | |
$vars[] = "blogger"; | |
return $vars; | |
} | |
add_filter('query_vars', 'blogger_query_vars_filter'); | |
function blogger_template_redirect() { | |
global $wp_query; | |
$blogger = $wp_query->query_vars['blogger']; | |
if ( isset ( $blogger ) ) { | |
wp_redirect( get_wordpress_url ( $blogger ) , 301 ); | |
exit; | |
} | |
} | |
add_action( 'template_redirect', 'blogger_template_redirect' ); | |
function get_wordpress_url($blogger) { | |
if ( preg_match('@^(?:https?://)?([^/]+)(.*)@i', $blogger, $url_parts) ) { | |
$query = new WP_Query ( | |
array ( "meta_key" => "blogger_permalink", "meta_value" => $url_parts[2] ) ); | |
if ($query->have_posts()) { | |
$query->the_post(); | |
$url = get_permalink(); | |
} | |
wp_reset_postdata(); | |
} | |
return $url ? $url : home_url(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment