Auto convert WordPress post content URLs to Hyperlinks and open in a new window

Posted on | January 26, 2011 | 2 Comments

I needed to have all of my post content plain text URLs converted to Hyperlinks, but it needs to open in a new window when clicked. The make_clickable() function in wordpress doesn’t allow to specify any option for the target.

function make_clickable2($content) {
    $content = preg_replace("/http:\/\/[a-zA-Z.&\-\/]+[a-zA-Z&\-\/]+/", "<a href=\"\${0}\" target=\"_blank\">\${0}</a>", $content);
    return $content;
}

add_filter('the_content', 'make_clickable2');

Comments

2 Responses to “Auto convert WordPress post content URLs to Hyperlinks and open in a new window”

  1. Roger Iyengar
    April 8th, 2011 @ 3:23 am

    An easier way would be to just use the links_add_target function. So if you used the code you would get the same result without adding your own function.

  2. Roger Iyengar
    April 8th, 2011 @ 3:25 am

    oh sorry, your site removed my code since I put it in php tags, but it would just be “links_add_target(make_clickable($link))”

Leave a Reply







Who Am I ?

I'm Marco Rodrigues and I currently work in Luxembourg as System Administrator.

Contact me.








Subscribe to my feed

Search

    Writing

    26 articles
    1176 comments

      Comments

      platforms
      windows 1128
      linux 21
      macos 3
      sun 0
      wp 0

      browsers
      ie 1059 
      firefox 57 
      opera 55 
      safari 15 
      chrome 11 
      epiphany 1 
      bonecho 0 
      flock 0 
      konqueror 0 
      iceweasel 0 
      lynx 0 
      netnewswire 0