Created
September 29, 2011 14:40
-
-
Save billerickson/1250866 to your computer and use it in GitHub Desktop.
Twenty Links - Redirect single posts
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
<?php | |
// in functions.php | |
// Find URL in post | |
function be_find_url( $content ) { | |
preg_match( '|href=["]([^\'^"]+)["]|mi', $content, $m ); | |
return $m[1]; | |
} | |
// Redirect single posts to linked URL | |
function be_redirect_on_single() { | |
if( is_single() ) { | |
global $post; | |
$url = be_find_url( $post->post_content ); | |
if( empty( $url ) ) return; | |
wp_redirect( $url, '301' ); | |
exit; | |
} | |
} | |
add_action( 'template_redirect', 'be_redirect_on_single' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment