Created
September 18, 2017 23:24
-
-
Save Mosharush/9c3169e2bce60b428e0241356f21629e to your computer and use it in GitHub Desktop.
Add target attribute to all links in post
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
/*** Add target attribute to all links in post ***/ | |
function gt_add_target_attr( $match ){ | |
if( strpos( $match[0], 'target=' ) === false ){ | |
return preg_replace( '#>$#', ' target="_blank">', $match[0] ); | |
} else { | |
return $match[0]; | |
} | |
} | |
function gt_add_target_to_links( $content ) { | |
$content = preg_replace_callback( '#<a.+href="(.+)".*>#mUui', 'gt_add_target_attr', $content ); | |
return $content; | |
} | |
add_filter( 'the_content', 'gt_add_target_to_links' ); | |
/*** End Add target attribute to all links in post ***/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment