1 comment

  1. 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' );
    

    Sidenotes:
    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.

Comments are closed.