Number of External Links in Comments – Moderation Option

I just ran over a problem to properly use the count of external (meaning no relative or absolute links to my own blog) links on my blog for the Comment Moderation count Option.

It’s labeled Hold a comment in the queue if it contains [your number here] or more links. (A common characteristic of comment spam is a large number of hyperlinks.) on Settings -> Discussion in the WordPress Back-end. Screenshot:

alt text

I’m aware that currently it counts all links inkl. links to the blog and other comments (reported it here: #14681) but I can’t imagine that there isn’t a plugin or hack already available that properly corrects the count to only external links.

So my question is: Is there a plugin / hack that makes WordPress properly count only the external links in comments for it’s Moderation Options?

2 Answers
2

Haha, I actually figured out a way to do this. As a plugin, this should work.

class JPB_CommentCounter {

  var $count = 0;

  function __construct(){
    add_filter( 'pre_comment_content', array( $this, 'content' ), 100 );
    add_filter( 'comment_max_links_url', array( $this, 'counter' ) );
  }

  function JPB_CommentCounter(){
    $this->__construct();
  }

  function counter( $num, $url ){
    if($this->count < 1)
      return $num;
    elseif( $this->count > $num )
      return 0;
    else
      return $num - $this->count;
  }

  function content( $content ){
    $homeurl = preg_quote( home_url() );
    if( preg_match_all( '@<a [^>]*href=[\'|"](/|'.$homeurl.')@i', $content, $matches ) )
      $this->count = count($matches[0]);
    return $content;
  }

}

$JPBCC = new JPB_CommentCounter();

I should add that I have not in any way tested this. But it should theoretically work.

Leave a Comment