Skip to content

Instantly share code, notes, and snippets.

@Mosharush
Created September 18, 2017 23:24
Show Gist options
  • Save Mosharush/9c3169e2bce60b428e0241356f21629e to your computer and use it in GitHub Desktop.
Save Mosharush/9c3169e2bce60b428e0241356f21629e to your computer and use it in GitHub Desktop.
Add target attribute to all links in post
/*** 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