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

Themes

Add additional meta boxes to Hybrid Theme

Published

on

At the time of writing this post I have Theme Hybrid as my parent theme, and am using a custom version of Hybrid News that Justin Tadlock offers to the public.

Well I want to tell you how you can add additional custom meta boxes to your child theme’s running the Hybrid Theme.

In your child function.php file just add the following to create a new post meta box:

/**
* Add additional post meta boxes
*
* by WPCult
*/
function wpcult_post_meta_boxes($meta_boxes) {
	$meta_boxes['image'] = array(
		'name' => 'image',
		'default' => '',
		'title' => __('Image:'),
		'type' => 'text',
		'show_description' => false,
		'description' => __('Please use a full size image, larger than 500px (this photo will dynamically re-size itself!'),
	);
	return $meta_boxes;
}
add_filter('hybrid_post_meta_boxes', 'wpcult_post_meta_boxes');

This will create a custom meta box called Image, and will create a custom field with the id of image.

Additionally you can create this on pages as well by copy/paste and replacing “post_meta_box” with “page_meta_box” every where inside the function.

Continue Reading

Tips & Tricks

Sick of images being to large for the content area?

Published

on

Have you ever uploaded an image that might have been a tad bigger than the width of your content area? I know I have! So here is a little CSS fix to handle these situations.

Resize those large images using CSS

Resize those large images using CSS

.post img {
	max-width: 600px;
	height: auto;
	}

That’s it, really simple huh. Just make sure that you change the .post to the correct body class(which starts with a “.“) or body ID(which starts with a “#“).

Would you like a quick explanation?
The CSS is just telling any image inside the class of .post to not be more than 600px wide. By the way make sure you change it to fit the dimension of your content width. And the height attribute is set to auto to automatically change the height to match the new width. 🙂

Continue Reading

Tips & Tricks

How to: Display RSS feeds from anywhere

Published

on

Here is a simple way to display any RSS feed in your WordPress blog.

<?php include_once(ABSPATH . WPINC . '/rss.php');
	$rss = fetch_rss('http://feeds2.feedburner.com/WPCult');
	$items = array_slice($rss->items, 0, 4);

		if (empty($items)) echo '<li>No items</li>';
		else
			foreach ( $items as $item ) : ?>
            <a style="font-size: 14px;" href='<?php echo $item['link']; ?>' title='<?php echo $item['title']; ?>'><?php echo $item['title']; ?></a><br /> 
            <p style="font-size: 10px; color: #aaa;"><?php echo date('F, j Y',strtotime($item['pubdate'])); ?></p>
            <p><?php echo substr($item['summary'],0,strpos($item['summary'], "This is a post from")); ?></p>
            <?php endforeach; ?>

The first step is to include the WordPress file rss.php then will apply code after to style the feeds.

Of coarse I forgot to mention, change the RSS feed to the feed you want to pull.

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Ceaser Emanuel Net Worth: How Rich is Black Ink Crew Star?

Ceaser Emanuel Net Worth: How Rich is Black Ink Crew Star?

David Emmanuel, who is better known as Ceaser Emmanuel, is the CEO of the tattoo shop franchise called Black Ink Crew. The show also has a reality television series of the same name on VH1 which has operated for a total of 8 seasons so far. As of 2019, Ceaser Emanuel net worth is estimated […]

Bernie Sanders net worth 2020.

Bernie Sanders net worth 2020.

Bernie Sanders net worth Introduction Bernie Sanders is an American politician who, since 2007, has been a junior U.S. senator from Vermont and U.S. One of the wealthiest guys to enter the potential presidential election of 2020 is Senator Bernie Sanders. Because of the intense emphasis of the political bourgeois on the role of ‘millionaires […]

Leah Messer Net Worth: How Rich is the Teem Mom Star Actually?

Leah Messer Net Worth: How Rich is the Teem Mom Star Actually?

Leah Messer is a television personality who is best known for being a part of the cast of the hit reality television show called Teen Mom 2. As of 2019, Leah Messer net worth is estimated to be $80,000. Messer grew up as a country girl from a small town called Elkview, West Virginia. She […]

Deval Patrick net worth.

Deval Patrick net worth.

Deval Patrick net worth Deval Patrick is an American politician who was born in Chicago, Illinois, in 1956. His father was a musician and left the family when Patrick was young. His mother raised him at the Robert Taylor Homes area near Chicago’s southern side. When he was in the 8th grade, Deval was recruited […]

Trending