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

WordPress Security Hacks

Published

on

By

Hi guys this is my first post on wpcult the great site Austin built.  Hope you guys find it usefull.

If you run a blog using the wordpress software then your blog is a target to hackers.  Below I will list some hacks and just how they can help you keep your business/site safe.

The following is  code to Block Bad Queries and protect your blog from malicious URL Requests.

Place the following code into a text file and name it what ever you like for example blockbadqueries.php upload it to your plugin folder and activate it in your wordpress admin just as you would any other Plugin

<?php
/*
Plugin Name: Block Bad Queries
Plugin URI:
http://perishablepress.com/press/2009/12/22/protect-wordpress-against-malicious-url-requests/
Description: Protect WordPress Against Malicious URL Requests
Author URI:
http://perishablepress.com/
Author: Perishable Press
Version: 1.0
*/
global $user_ID; if($user_ID) {
  if(!current_user_can(‘level_10’)) {
    if (strlen($_SERVER[‘REQUEST_URI’]) > 255 ||
      strpos($_SERVER[‘REQUEST_URI’], “eval(“) ||
      strpos($_SERVER[‘REQUEST_URI’], “CONCAT”) ||
      strpos($_SERVER[‘REQUEST_URI’], “UNION+SELECT”) ||
      strpos($_SERVER[‘REQUEST_URI’], “base64”)) {
        @header(“HTTP/1.1 414 Request-URI Too Long”);
 @header(“Status: 414 Request-URI Too Long”);
 @header(“Connection: Close”);
 @exit;
    }
  }
}
?>

 This Great plugin was made by Jeff Starr of Digging into WordPress

 

 

Protecting your blog with .htaccess 

.htaccess files have lots of possibilities. below is some code that will help protect your wordpress from modification of _REQUEST and/or GLOBALS and scripts injection.

 This is real simple just paste the following code into your .htaccess file. Always make a backup of your .htaccess before editing, better to be safe.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

 

Thanks to Oussama for this great hack

Continue Reading

Tips & Tricks

How to: Create a fbshare.me shortcode

Social networks are everywhere. I am sure you’re on facebook. Well why not at a facebook share script to your site?

Published

on

Don’t know PHP that well?

Well here is a simple way to add a share script like fbshare.me to your site, via shortcodes.

Paste the following code in your functions.php file in order to create your shortcode:

function fbshare_script() {
     return '<div class="fbshare"><script src="http://widgets.fbshare.me/files/fbshare.js"></script></div>';
}
add_shortcode( 'fbshare', 'fbshare_script' );

Once done, you can display the facebook share button anywhere on your posts. In WordPress editor, make sure you are in HTML mode and insert the following: [fbshare].

When your post will be published, the shortcode will be replaced by the fbshare.me button.

Continue Reading

Tips & Tricks

Turn your RSS feed into a shortcode

Published

on

Last week I wrote how to “Use WordPress to print a RSS feed for Eventbrite attendees“. It was pretty popular, but then I found myself in a place that was more annoying. Trying to incorporate that into a blog post or page.

Without having to download a plugin that will allow PHP to be executed inside a post, I would have to create a template file and use that. Which is what I did, and it works just fine. But for some reason I forgot all about shortcodes! With a shortcode, I could generate all the PHP in the functions file and then just call the shortcode when/where I want.

Okay, so lets show the completed PHP code:

function attendee_feed_print_2009() {
 global $wpdb;
 include_once( ABSPATH . WPINC . '/rss.php' );
 $rss = fetch_rss( 'http://www.eventbrite.com/rss/event_list_attendees/384870157' );
 $items = array_slice( $rss->items, 0 );
 if ( empty( $items ) ) echo '<ul style="list-style-type: none; list-style-image: none; list-style-position: outside;"><li>No items</li></ul>';

 else

 foreach ( $items as $item ) : ?>
 <ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-bottom: 0px">
 <li><!--<strong><?php echo $item[ 'title' ]; //User name ?></strong><br />-->

 <?php echo $item[ 'content' ][ 'encoded' ]; ?>

 <hr style="border: 1px solid #ddd; margin-bottom: 8px" />
 </li>
 </ul>
 <?php endforeach;
}

Now, this code has to be changed for it to work as a shortcode. We’ll have to return the function and not print/echo it.

I also wanted to be able to use multiple instances of the code with different feeds. To do so I had to create a argument to extract from the completed short code. I found a demo at: Alex Mansfield’s post.

Remember this:

function attendee_feed_print_2009() {
 global $wpdb;
 include_once( ABSPATH . WPINC . '/rss.php' );
 $rss = fetch_rss( 'http://www.eventbrite.com/rss/event_list_attendees/384870157' );

We are going to update it to read as follows ( changes in bold ) :

function attendee_feed_print_2009( $rss_nbr ) {
 global $wpdb;
 extract( shortcode_atts( array( 'rss' => ''), $rss_nbr ) );
 include_once( ABSPATH . WPINC . '/rss.php' );
 $rss = fetch_rss( $rss );

And the final code with the fields updated to return the arguments ( put into your functions.php file ( in between <?php ?> ) ) :

function attendee_feed_print_2009( $rss_nbr ) {
  global $wpdb;
  extract( shortcode_atts( array( 'rss' => ''), $rss_nbr ) );
  include_once( ABSPATH . WPINC . '/rss.php' );
  $rss = fetch_rss( $rss );
  $items = array_slice( $rss->items, 0 );
  
  $rss_html = '<div id="eventbrite-attendee-list" style="clear:both;">';
  
  if ( empty( $items ) ) $rss_html .= '<ul style="list-style:none;"><li>No attendees, yet.</li></ul>';

  else

  foreach ( $items as $item ) :

  $rss_html .= '<ul style="background:none; list-style:none; margin:0px">';
  $rss_html .= '<li style="background:none; list-style:none;">';
  $rss_html .= $item[ 'content' ][ 'encoded' ];
    
  $rss_html .= '<hr style="border: 1px solid #ddd; margin-bottom: 10px" />';
  $rss_html .= '</li>';
  $rss_html .= '</ul>';
  
  endforeach;

  $rss_html .= '</div>';

  return $rss_html;
  }

And lets not forget to add the shortcode function!

add_shortcode( 'eventbrite-attendees', 'attendee_feed_print_2009' );

Final outcome with look like this `[eventbrite-attendees rss="http://your-rss-feed.com/"]`

I’ve created this into a plugin!

Download the plugin: http://wordpress.org/extend/plugins/eventbrite-attendees-shortcode/

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

David Bromstad Net Worth: How Rich is David Bromstad Actually?

David Bromstad Net Worth: How Rich is David Bromstad Actually?

David Reed Bromstad is an American designer and television personality who is best known for being the host of the show called Color Splash with David Bromstad. Moreover, he is also the host of HGTV’s My Lottery Dream Home helping lottery winners to buy their dream real estate. As of 2019, David Bromstad net worth […]

FaZe Jarvis Net Worth: How Rich is FaZe Jarvis Actually?

FaZe Jarvis Net Worth: How Rich is FaZe Jarvis Actually?

FaZe Jarvis is an English Fortnite player. He is the brother of popular YouTuber FaZe Kay of the Call of Duty gaming group FaZe Clan. As of 2019, FaZe Jarvis net worth is estimated to be $600,000. FaZe Jarvis was born on November 11, 2001, in England. He is more of a new streamer. He […]

Jessica Nigri Net Worth – Hot Gossip and info

Jessica Nigri Net Worth – Hot Gossip and info

Jessica Nigri Net Worth Although she was brought up in New Zealand, Jessica Nigri is from America and she is a cosplay celebrity, voice actress, social media personality, and model. She has a net worth of close to $15. Million. Jessica was born in the U.S. state of Nevada in Reno in August of 1989. […]

Michael Imperioli net worth.

Michael Imperioli net worth.

Michael Imperioli net worth James Michael Imperioli is an actor and TV writer with Italian-American origins. Michael has a net worth close to $25 million. Michael was born in March 1966 in the Mount Vernon area of New York. He grew up in the different areas of New York City, such as New Jersey, Ringwood, […]

Trending