Connect with us

Tips & Tricks

How to: Open external links in a new window

Published

on

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.

Update

If you like you can remove the var $j = and replace all $j with simply just $

 

Continue Reading
7 Comments

7 Comments

  1. Shawn Parker

    March 1, 2009 at 5:53 pm

    Good tip, thanks for picking this up after my blunder. A couple more things to note:

    1. Instead of worrying about jQuery’s noConflict you can simply use jQuery(…) instead of $ – then everything you do is safe even if jQuery doesn’t get started in no conflict mode.

    2. jQuery(document).ready(function() { can be replaced with just jQuery(function() {

    3. To be xhtml future proof you can’t use target=”_blank” any more. Something like this would be more appropriate:

    jQuery(function() {
    jQuery(“a:not([@href*=http://YOURSITE.com/])”).not(“[href^=#]”)
    .addClass(“external”)
    .click(function(){
    window.open(this.href); // pop a new window
    return false; // return false to keep the actual link click from actuating
    })
    });

    • Frosty

      March 1, 2009 at 6:38 pm

      Thanks!

  2. Pingback: Open Links in New Windows or Tabs Without Target=”Blank”

  3. Elena Coen

    May 8, 2009 at 5:24 am

    Hi there,

    Is there a hack for this that would allow links from a folder to open automatically in a new window? I have an RSS feed from another install I did in a folder. I want anything from there to open in a new window but because it starts with the same URL, such code recognizes it as a match and keeps it opening in the same window. So http://www.mysite.com/anything else would open in same window, any external URLS plus http://www.mysite.com/jobs I want to open in a new window.

    Thank you, Elena

    • Austin

      May 8, 2009 at 11:44 am

      Hmmm.. This may be possible with some anchor not attributes in jQuery. Let me browse around and see what I can come up with..

  4. Udegbunam Chukwudi

    September 21, 2009 at 1:54 am

    Wonderful tutorial. Would definitely come in handy for some folks. For me, when I link to old posts on my blog from a new post, I like them opening in a new window so the reader doesn’t get lost trying to find his/her way back to the start.
    BTW is there a way of making this code force all links to open in a new window?
    .-= Udegbunam Chukwudi´s last blog ..Top 10 Ways Nigerians Make Money Online =-.

  5. Pingback: darrinb.com | opening external links in a new window using jquery

You must be logged in to post a comment Login

Leave a Reply

Tips & Tricks

Remove spaces when echoing the_title

Ever wanted to print or echo the WordPress title attribute without spaces?

Published

on

This little trick can be useful for calling custom functions and printing the title with out spaces for W3C compatibility. I used this trick in a new theme called Galleria, which will be out for public download in the coming days.

Using this comes in handy for a delicious text link:

<?php $title = get_the_title(); ?>
<a href="http://del.icio.us/post?url=<?php echo $title; ?>&amp;<?php echo str_replace(" ", "%20", $title); ?>">
Bookmark This (<?php echo $title; ?>)</a>

What I am doing is calling $title = get_the_title(); and using str_replace(" ", "%20", $title); to replace empty spaces with a %20, which is used in URL encoding empty spaces.

Alternatively you can use a dash or underscore.

Thanks to Jason Boyle for his adaption.

Continue Reading

Tips & Tricks

Display the_excerpt only if there is text

Have you ever wanted to display the excerpt only if you write one? A simple couple lines of code can display the_excerpt any where you like.

Published

on

In my new theme, I am using this coded trick to display the excerpt on a single post only if I’ve got text inside. Usually if you use the_excerpt and you don’t have one, it will fake one for you.

This is not something that I wanted to do on the single post page. So I used the following code to check if the excerpt existed.

if ( !empty( $post->post_excerpt ) ) :

Once this action is taken into account, you can factor in what code you want to out put if the post_excerpt isn’t empty.

if ( !empty( $post->post_excerpt ) ) :
	the_excerpt();
else :
	false;
endif;

The above code checks if there is an excerpt and print’s it to the screen. If there isn’t an except, it doesn’t do anything.

Continue Reading

Tips & Tricks

Add a shortcode

Published

on

This is a simple one.

/**
 * Your Blog title
 *
 */
function my_blog_title() {
	$blogname = get_bloginfo('name');
    return '<span class="blog-title">' . $blogname . '</span>';
}		  	
add_shortcode('blog-title', 'my_blog_title');

Just add this to your functions.php file and then add [blog-title] in any post or page and it will return your Blog Title. :)

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Azriel Clary Wiki, Parents, Bio: Where is Azriel Clary Now?

Azriel Clary Wiki, Parents, Bio: Where is Azriel Clary Now?

Azriel Clary is an American singer and one of R. Kelly’s alleged sex cult victims. Azriel met R. Kelly at the age of 17 and she convinced her parents to go on tour with him. Her parents allowed her to go on a trip with R. Kelly with a belief that he would mentor in […]

Mike Bibby Net Worth.

Mike Bibby Net Worth.

Mike Bibby Net Worth. Among the world-famous basketball players, Mike Bibby is undeniably one of the top players. He is, at present, a global personality. Mike Bibby is a national champion of the year 1997 and a Pac 10 1998 player in Arizona. He is one of the most highly regarded players of the National […]

Chelsea Houska Net Worth: How Rich is the ‘Teen Mom’ Star?

Chelsea Houska Net Worth: How Rich is the ‘Teen Mom’ Star?

Chelsea Anne Houskais an American television personality who is best known for being a part of the reality television show “Teen Moms 2.” Before this, she had been a part of the show called 16 and Pregnant. As of 2019, Chelsea Houska net worth is estimated to be $80,000. Houska was born on August 29, […]

Tall Guy Car Reviews Net Worth: How Rich is the YouTube Star?

Tall Guy Car Reviews Net Worth: How Rich is the YouTube Star?

Tall Guy Car Reviews Net Worth and Bio: Corey Barrett is an American YouTube personality from Minneapolis, MN who runs the famous channel ‘Tall Guy Car Reviews’. He basically publishes video content related to car reviews and sometimes about his personal life. As of 2019, Tall Guy Car Reviews’ net worth is estimated to be […]

Trending