Here’s a little plugin to fix an annoying problem. Do you ever have commenters paste an entire URL into their comment as plain text, rather than putting it in <a href="..."> tags? Or if they do use the a tags, do they still use the URL as the link text?
Sure looks ugly when people do that. Here’s a little plugin to replace URLs that occur in comments with [link]. We don’t strip the URL–the word [link] will be clickable–we just make the comment prettier. Enjoy:
<?php
/*
Plugin Name: KB Comment Links
Plugin URI: http://adambrown.info/b/widgets/tag/kb-comment-links/
Description: Replaces long, ugly URLs in comments with a short link
Author: Adam R. Brown
Version: 1.0
Author URI: http://adambrown.info/
*/
function kbcl_comment_filter($comment){
if (false===strpos($comment,'http://'))
return $comment;
// links in <a> tags using URL as link text
$comment = preg_replace('~(<a[^>]+>)(http://\S+)</a>~', '$1[link]</a>', $comment);
// links that aren't in <a> tags
$comment = preg_replace('~(\s|<p>)(http://\S+)(\s|</p>)~', '$1<a href="$2">[link]</a>$3', $comment);
return $comment;
}
add_filter('comment_text', 'kbcl_comment_filter');
?>