So for example: if I post http://testeteststetetst.com, it won't make it a link if it's a post. If it's comment, it does make it a link... Now, I want it for posts too.. Is this possible, if so, how? Can someone help me with that please?


I use a similar method to the following in a plugin of mine:

function wpse107488_urls_to_links( $string ) {
    /* make sure there is an http:// on all URLs */
    $string = preg_replace( "/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2", $string );
    /* create links */
    $string = preg_replace( "/([\w]+:\/\/[\w-?&;%#~=\.\/\@]+[\w\/])/i", "<a target=\"_blank\" title=\"" . __( 'Visit Site', 'your-textdomain' ) . "\" href=\"$1\">$1</a>", $string);

    return $string;

I don't use it for post content, but it should work there.
For that, you'd have to employ the the_content filter:

add_filter( 'the_content', 'wpse107488_urls_to_links' );

This is untested.
The regexes are fairly good, but they will fail in niche cases once in a while. Identifying a URL by format only, while avoiding false positives isn't the simplest of things.

  • Nope, it is nothing working... Of course I mean with that: it breaks my other non-http links. For example, I have inline links that link to posts (which don't use http but a shortcode), but now, with this function, it gives them also an http:// and a title, and here, the title is visible in the post instead in the hover... Can you enhance the code? – Johan Jul 23 '13 at 13:58
  • It does what it has to do, so it makes a link of http:// code, but the inline links without http:// are getting http:// before them and a title in the text... can you write an exception for http:// only? – Johan Jul 23 '13 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.