Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created September 29, 2011 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/1250866 to your computer and use it in GitHub Desktop.
Save billerickson/1250866 to your computer and use it in GitHub Desktop.
Twenty Links - Redirect single posts
<?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