Looking to automatically open WordPress links in new tabs using the_content()? All you may need is to copy and paste this code into your functions.php file.
2 answers
1
Automatically opening the_content() links in new tabs
To automatically open links in new tabs when using the_content(), simply copy and paste in the code below into your WordPress theme’s functions.php file.
function automatic_add_target_blank( $content ) {
$post_string = $content;
$post_string = str_replace( '<a', '<a target="_blank"', $post_string );
return $post_string;
}
add_filter( 'the_content', 'automatic_add_target_blank' );— Support WizardAnswer 1
0
Automatic new tab links for blog posts only
To only allow the plugin to work for single-blog posts, simply update the above code with an if statement to detect for singular posts, as per below.
function automatic_target_blank( $content ) {
$post_string = $content;
if ( is_singular( 'post' ) ) {
$post_string = str_replace( '<a', '<a target="_blank"', $post_string );
}
return $post_string;
}
add_filter( 'the_content', 'automatic_add_target_blank' );— Support WizardAnswer 2