Number of External Links in Comments – Moderation Option

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