WordPress – always open sidebar links in new window

I recently had a client that thought it was too much work to always have to select the “_blank” target open when adding new links to WordPress. I Googled around and saw a lot of simple solutions but they all involved complex setup or customizing core wordpress code – which I hate because it breaks with every upgrade.

What I need turned out to be simple. A few lines of JavaScript and the jQuery library (which was available in the theme already) and before Masterchef had finished everything was working exactly as the client wanted and I was confident that the theme and the wordpress install would be stable and upgradeable.

Here is the function as I wrote it (in a theme that already included jQuery and had the noConflict setting)

<script language="javascript">
jQuery(document).ready(function() {
    jQuery('ul.blogroll li a').each(function(i)
       {
          jQuery(this).attr('target', '_blank');
       }
    )
});
</script>

If you had to implement from scratch you’d need to download (or include it straight form Google) and include jQuery and your code would probably need to run like

<script type="text/javascript" src="jquery.js"></script>
<script language="javascript">
$(document).ready(function() {
        $('ul.blogroll li a').each(function(i)
        {
            $(this).attr('target', '_blank');
        }
    )
});
</script>
VN:F [1.9.17_1161]
Rating: 0.0/5 (0 votes cast)
Share the Love
Get Free Updates

Related posts:

  1. Open links in a new window and still validate
  2. WordPress Development
  3. Make Google Friend Connect Work with Thesis
  4. What is the best blog platform for a business ?
  5. Integrating Big Commerce and WordPress

Tags: ,

Leave a Reply