Over the weekend, I attended WordCamp Denver, and I was asked by John Hawkins how to force links to open in a new tab with out editing the source code. So, today lets learn a simple jQuery trick to open all external links in your site in a new tab or window. We are going to make sure you have jQuery active on your site, you can do this easily in WordPress, since it’s bundled with the latest installations. Use this code in your header: <?php wp_enqueue_script('jquery'); ?>
then, below the wp_head
add the following:
<script type="text/javacript"> var $j = jQuery.noConflict(); $j(document).ready(function() { //external attribute $j("a:not([@href*=http://YOURSITE.com/])").not("[href^=#]") .addClass("external") .attr({ target: "_blank" }); } ); </script>
That’s it, just make sure you change the http://YOURSITE.com to your website.
If you like you can remove the var $j =
and replace all $j
with simply just $
Hey, do you have a list a social bookmark links? Does it have a Twitter…
The WordPress plugin repository has over 54,000 plugins that enhance user experience by filling gaps…
We all want to read comments, especially if these comments praise us or show that…
Videos add life to blogs. It could make explaining things easier and deliver the message…
More information on Misspelled SEO Keywords If appropriately used, misspelled keywords can be worth gold.…
WordPress Backup is a Must! Creating WordPress Backup regularly is one good way to add maximum security…