Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I now wrote a complete plugin for this that offers the basic link counting interface for adding comments with wordpress 3.0. In addition to what John proposed I made some changes / addings based on tracing the hooks triggered by adding the comment as I wanted to get this only executed when a new comment is actually added.

I therefore did chose the comment_text instead of the pre_comment_content hook. The rest is basically the same but some sanity checks were added so that it only get's executed once and on adding a new comment only. An additional (but currently unimplemented) check would be to enable it only after the comment_max_links option has been read by get_option()(the pre_option_comment_max_links-hook), but as for the moment this did pass testing, I have not done it so far.

The sourcecode can be viewed online in trac.

I'll give the answer to John even I did not choose his suggestion as he posted the same principle here first.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top