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.
Related Posts:
- How does WP decide how to display a page
- External link not showing on post but showing on comment
- How to use “Add link” pop up for a WordPress widget
- How to make comments work for a post loaded per Ajax?
- How to add category to: ‘wp-admin/post-new.php’?
- How to allow hidden custom fields to be added from wp-admin/post.php?
- Delete duplicated wp_postmeta record
- Keep getting same permalink with WP_Query?
- Change slug with custom field
- How to display comments and comment form on custom post type?
- Comments are assigned to wrong or related post
- How can I allow editors to leave comments on posts that have not yet been published?
- Update post date on every new comment?
- Better post meta efficiency?
- Edit meta data does’t work with custom sql
- Comments enabled, but disabled at the same time
- Human Time Diff, change mins to minutes
- Enable commenting on front-end preview page for pending posts
- Adding Multiple Values to a Post Meta Key
- Using radio button meta data from a custom meta box
- Disable Post/Page comment emails for creators
- PHP Notice error (when on 404 page)
- Why is WordPress redirecting users to random posts after commenting?
- Adding a text link under the post title in the lists of posts
- Checking if a post with certain meta value exists
- Setting up navigation links for posts (first, prev, next, last)
- Diplay comment date on WP_Post_Comments_List_Table
- Modify WP_Post before processing
- I would like to give special promotion for the first 100 posts in my blog? Can anyone tell me how to do that?
- Change post_date to post_modified date on post template?
- Converting a dynamic piece of code using WordPress Loop into a static one using Post ID
- Removing rel=”nofollow” from links posted in Post Content
- Delete post by giving its link
- is there a way to show the the post title after the image?
- Emojis replaced by ‘?’ automatically
- get_adjacent_post by language
- How to handle broken links created by permalink/slug changes?
- A friend wants to make a comment on one of my articles, she has to be logged to wordpress?
- Customizing Comments on Posts
- Cannot retrieve a custom RSS field from posts
- Saving custom fields to a custom taxonomy
- What do comments with […] mean?
- How to get posts and comments amount per hour, per year and per month?
- Change all author links in Blog roll
- How to I retrieve the ID from the Posts page?
- How do you change the comment count in the back end posts list, to reflect unapproved comments, rather than all comments?
- Is there any way to tell when wp_postmeta has been updated?
- Let both the admin and post author update comment meta
- Can’t get full post title if there a spaces in title
- How to style archive post titles… but only those posts who have comments?
- Display post number by category
- Add multiple meta keys to a post at once
- Show number of posts by logged in user
- Exists filter or action that change Add New Post link?
- Display on page every post in blog WITH comments
- Insert data from custom created PHP page into wp_postmeta table
- Showing recent post of category in page
- saving/reading custom field value does not work – no value gets POSTed
- Internal linking to posts permalink fail because of spaces and stripe at postname
- Link from Post to Another Post Via Clicking on Words within Original Post
- Link to older links
- How to create several Post pages
- How to implement a WordPress comments function?
- Count the total views of all user posts published
- Edit post & page option does not display on latest wordpress
- Shortcode for Comments and Comment Boxes in Post Loop
- Select another post in a post meta like a parent page is selected while editing a page
- WordPress 5: prevent link from displaying page title instead of URL
- Some custom post types did not return an object
- Remove the URL author of post on all comments
- Blogposting Schema & Structured Data without plugin
- Automatically select template based on query string for New Posts
- WordPress weird behaviour on comments
- Comments are in database, don’t show up in WordPress backend or frontend
- Add postmeta to all blog posts
- WP_Query sort by meta_value_num or date
- Grab meta data before post is saved
- How to fetch posts that are stored in the different table than the default table (wp_posts)?
- Check if user has avatar
- Filter question list on substring of metavalue
- Add a meta to a post submited from a frontend form
- How to enable comments meta box for other author posts?
- Post List Widget with custom posts and editing the Read More Link
- Comments as part of a post
- Run function after post is updated
- 3rd Party Login in wordpress
- My posts section for logged in user
- Apply comment to different post (not the current post) [duplicate]
- Adding custom fields to the Quick Edit screen – puzzled about the column concept
- Display the first post’s comments of category in comments.php template
- How to display a certain template element only for posts published within certain time range?
- How enter custom post meta or custom field on box post list archive page?
- Automatically add class to link based on link text
- Read more link not working [closed]
- WP_query sort by custom meta_key “price”
- After the local installation of an old WP website I can see the homepage but I can’t access to the articles, why?
- How does WordPress add the comments and comment form to posts
- NewsPaper WP Theme – Additional Related Posts Filter by Time (divTag composer)
- Site ‘Categories’: save an admin global setting with post metadata [closed]
- Best method for comment section associated with different images?