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

Adding a external file after the first post

Published

on

How might you display a Google ad after the first post or anything you like? It is very simple. You just need to add the variable $loopcounter in the Loop. If the $loopcounter is less than or equal to 1, then include your option. Check out the code:

<?php if (have_posts()) : while (have_posts()) : the_post(); $loopcounter++; ?>

  // your loop

  <?php if ($loopcounter <= 1) { include (STYLESHEETPATH . '/you-file.php'); } ?>

<?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

Pretty simple huh. Well in the $loopcounter line, you may change the include to point to any file or maybe a custom widget like so:

<?php if ($loopcounter <= 1) { dynamic_sidebar( 'Plus Post' ); } ?>

Or use your code directly in between the { & }.

Continue Reading

Tips & Tricks

A simple way to query posts

Published

on

Here is a simple way to call query_posts with an array of options. For all options you my use visit: WordPress Codex.

<?php $my_query = array('showposts' => 4, 'post__not_in' => $do_not_duplicate); ?>

    <?php query_posts($my_query); ?>
Continue Reading

Tips & Tricks

Adding a favicon to your site

Published

on

Looking to add a favicon to you site?

Inside your WordPress theme’s functions file (functions.php) add the following to inside your PHP code.

/**
 * FAVICON
 * @WPCult.com
 */
function my_favicon() { ?>
	<link rel="shortcut icon" href="<?php echo bloginfo("stylesheet_directory") ?>'/images/favicon.ico" />
<?php }

add_action('wp_head', 'my_favicon');

That’s it, just be sure to upload an icon image or a .gif/.png. Be sure to correct the target location id the file is located somewhere else.

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Andrew Yang Net Worth

Andrew Yang Net Worth

Who is Andrew Yang? Andrew Yang is an American Entrepreneur best known as the founder of Venture for America. He is also the U.S 2020 Democratic presidential candidate. Yang’s Early Life Yang was born on January 13, 1975, in Schenectady, New York to immigrant parents from Taiwan. His father worked in the research labs of […]

Buck Henry Net Worth: How Rich was Buck Henry Actually?

Buck Henry Net Worth: How Rich was Buck Henry Actually?

Buck Henry was an American actor, screenwriter, and director best known for his directing the movie Heaven Can Wait. Buck Henry net worth before his death is estimated to be $6 million. Henry was born on December 9, 1930, in New York City to Ruth Taylor and Paul Steinberg Zuckerman. His mother was an actress […]

Dolores Catania Net Worth: How Rich is Dolores Catania Actually?

Dolores Catania Net Worth: How Rich is Dolores Catania Actually?

Dolores Catania is an American Entrepreneur and Reality Television star best known for being a cast member on “The Real Housewives of New Jersey”. As of 2019, Dolores Catania’s net worth is estimated to be $2 million. Catania was born in Patterson, New Jersey, on December 28, 1970. Her parents are of Italian roots. Her […]

Blueface Net Worth: 5 Interesting Facts About the Rapper

Blueface Net Worth: 5 Interesting Facts About the Rapper

Blueface Bleedem, or simply Blueface, is an American rapper from Los Angeles currently signed to Birdman’s Cash Money West label. He rose to fame after releasing single ‘Deadlocs’ on music platform SoundCloud. The song has received more than 2.5 million streams. Here are some of the interesting facts about Blueface. 1. Blueface net worth is […]

Trending