Today I’ll show you how to shorten comment links, so we will talk about the comment_text function.

The problem

Did you noticed that on a lot of responsiv sites, when a visitor paste a long link in a comment, the layout badly react and sometimes adds a horizontal scrollbar. If it does not, it is not less ugly to have an full URL displayed in clear. This was the case on bweb – example here – until I take care of the subject.

The reason of this little problem is quite simple, an URL pasted in a comment is natively transform into link, but an URL is a sequence of characters with no spaces (and sometimes without dashes) so the browser interprets it as a single word and do not break the line.

The solution is actually quite simple, WordPress provides to us the comment_text function. The latter is already filtered by many functions as follows:

add_filter( 'comment_text', 'wptexturize' );
add_filter( 'comment_text', 'convert_chars' );
add_filter( 'comment_text', 'make_clickable', 9 );
add_filter( 'comment_text', 'force_balance_tags', 25 ); 
add_filter( 'comment_text', 'convert_smilies', 20 );
add_filter( 'comment_text', 'wpautop', 30 );

 

For our needs, we’ll add a new filter that will be applied after the URLs have been converted into links. The principle is simple, the new filter will parse the content returned by comment_text through a regular expression (REGEX) – just like for the_content – to retrieve the link, then shorten it and finally create a new one shorter and  less unsightly.

 

The solution

To use this filter, paste it in the files where your theme functions are located.

function bweb_comment_link_shortener($matches) {
    $short_url = preg_replace('~^([^/]*)/(.{5})(.{3,})(.{10})$~', '$1/$2...$4', $matches[2]);
    return '<a href="' . $matches[1] . '" target="_blank" title="URL: ' . $matches[1] . '">' . $short_url . '</a>';
}
function bweb_detect_comment_url($content) {
	$pattern= '/<a.*href=\"(https?:\/\/.*)\".*>https?:\/\/(.*)<\/a>/iU';
	$content= preg_replace_callback($pattern, 'bweb_comment_link_shortener', $content);
	return $content;
}
add_filter( "comment_text", "bweb_detect_comment_url" , 9);

 

Note that the filter is applied on the comment displaying and not during it record, so you can seamlessly edit/delete the function later.

Post written byBrice CAPOBIANCO

WordPress addict and self-taught. I love to learn and to create, then to share…
Founder of bweb.
Share this post

Your email address will not be published. Required fields are marked *

Show 6 comments

6 comments

  1. Thanks for this, man, I’ve been getting Google Console alerts for mobile usability issues for weeks now because some of our blog posts had super long URLs in the comment section and they messed up small screens. I was looking for plugin based solutions but none came up, that’s when I ventured onto your site. I put your code at the end of my functions.php and it works beautifully. I just sent off my revalidation request in Google Console. Thank you so much!

  2. My site installedz askimet and grow anti spam plugin which is auto approve all comments. I think with these two both plugins might only real people can put comments on my site.But I was wrong. They build a lot of backlinks again and again.
    I have some other blogs which is no spam/no comments because I was setting up there a captcha below comment section and I think this may be caused I didn’t see any comment on my old blogs.
    So, I decided to change the comment box more friendly to get attention from people until a lot of spams coming.
    Thank you so much. Brice!for helping me this out.

  3. Hi Brice! How can I replace or shorten an URL inside the URL field comment. Many visitors trying to spam my website by comment some affiliate links to build their backlinks. The pain point is they put their backlinks and keywords when commenting on my site.
    I’m trying to stop their links pointing out of my sites.
    Does it have a way by shorten and no-index, no-follow those kind of links?
    Thanks.

    • Hi William,

      First of all, if you don’t already use an anti spam plugin, I suggest you to use this on. It works really fine for me.

      Then, I personnaly don’t let people leave comments before approvale, and if I feel that the site URL is filed for SEO purpose, I edit the comment and delete it (just keep the comment author name).

      Finally, if you want to add no-index/no-follow to the above function, feel free to edit it!

      You may try something like that :

      function bweb_comment_link_shortener( $matches ) {
      $short_url = preg_replace('~^([^/]*)/(.{5})(.{3,})(.{10})$~', '$1/$2...$4', $matches[2]);

      $class = 'internal';
      $target = '_self';
      $attr = '';

      $siteUrl = parse_url( get_site_url() );
      $matchedUrl = parse_url( $matches[1] );

      if( $siteUrl[ 'host' ] != $matchedUrl[ 'host' ] ){
      $target = '_blank';
      $class = 'external';
      $attr = 'rel="nofollow"';
      }
      return '' . $short_url . '';
      }
      function bweb_detect_comment_url( $content ) {
      $pattern= '/https?:\/\/(.*)<\/a>/iU';
      $content= preg_replace_callback( $pattern, 'bweb_comment_link_shortener', $content );
      return $content;
      }
      add_filter( "comment_text", "bweb_detect_comment_url" , 9);