Add the code in this article to your functions.php file to add target=”_blank” to your links.
/*
* Add target blank to content links
*/
function wp_add_target_blank($content) {
// Get the site url
$site_url = get_site_url();
// Get all the links in the article
preg_match_all('#<a ([^>]*)>#i', $content, $matches);
// Loop through each link
foreach ($matches[1] as $key=>$value)
{
// If link does not contain the site_url or the text _blank, add target="_blank"
if (!stristr($value, $site_ul) && !stristr($value, '_blank'))
{
$content = str_replace('<a '.$value.'>', '<a target="_blank" '.$value.'>', $content);
}
}
// Return the modified content
return $content;
}
// Add the wordpress filter
add_filter( 'the_content', 'wp_add_target_blank' );
Code language: PHP (php)