Open All Links in New Windows add target blank in all a html elements

Open all links in new windows, WordPress add target blank in all (a) HTML tags.

I am personally not a fan of opening links in new windows – I think it should be left for the user to decide. But I’m not one to judge, so if you want to do it, use this simple code snippet on your WordPress theme’s functions.php file.

WordPress code snippet:

/**
 *
 * Add target blank in all a html elements
 */
if ( ! function_exists( 'ns_auto_blank' ) ) {
function ns_auto_blank( $text ) {
	$return = str_replace( '<a', '<a target="_blank" ', $text );
	return $return;
}
add_filter( 'the_content', 'ns_auto_blank' );
}

 

Find out more about functions here

Thank you for seeing my tutorial and feel free to share and comment :). Do you have a code snippet and you want to see it published on my site? I will be more than happy to do it please send me a message (here)

ABOUT AUTHOR

Nuno

I am a Freelance Web Developer and WordPress Expert based in London with a wealth of website development and support experience. I am great at problem solving and developing quick solutions.

  • LET'S TALK!

    Fill in the form below to make an enquiry or find my contact details on my contact page.

  • This form collects your name and email address so that I can respond to the enquiry(ies) that you submit via this website. Please check my privacy policy for the full story on how I use, protect and manage your submitted data.