public
Created

Twenty Links - Redirect single posts

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?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' );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.