3

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?

5
  • This is a tough one to crack. It's easy enough to make internal links invisible to the link checker, but impossible to get them back to their original state before they're inserted into the database. Unless you hack core. Which you should never do, unless it's to make a patch. Commented Aug 24, 2010 at 13:21
  • No I don't want to hack core. For "hacking" core I created the bug report in #14681. Maybe my description of the question is bad. I will add some screenshot.
    – hakre
    Commented Aug 24, 2010 at 15:01
  • No, I understood your question. I looked through core, and it's just not possible without hacking core. The ticket would fix the issue, but there's no way to do what you're asking about as of now. Commented Aug 24, 2010 at 15:29
  • Oh it's possible w/o cracking core, I'm pretty sure. Just waiting for a customer to show good faith. I'll post the code here if so (even you're right and I fail).
    – hakre
    Commented Aug 27, 2010 at 22:03
  • Plugin is in the pipe to be published on wordpress.org/extend I just need to upload it, it's working already. hakre.wordpress.com/plugins/comment-link-count
    – hakre
    Commented Nov 4, 2010 at 2:43

2 Answers 2

1

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.

1
  • Cool thanks for posting. I started something with the same logic behind two days ago but was not able to finish because of other things to do. I'll look for your way as well and might put the two in the mix. I'll add a non-strict domain / subdomain check against links from a firefox plugin I needed to hack for hakre.wordpress.com/2010/06/16/… // addons.mozilla.org/de/firefox/addon/11787 . Thanks!
    – hakre
    Commented Sep 4, 2010 at 11:50
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.