I've been trying to set something that might be really easy (but I never had to do this before) :

add_filter('pre_comment_user_ip', 'limit_commentator');
function limit_commentator( $comment ) {
    $comment_field_value = get_comment_meta( $comment->comment_ID, 'some_custom_field', true );
    //my code


I think this is the right hook but how can I limit the ability for commentators to post comment WITH this custom field? Actually I just want one value per IP.

Got another question : Is it really worthy to limit per IP? I've been thinking about a limit on a daily basis. What do you think about this?


That hook won't do it. It is a filter. You can alter the IP data but unless you are willing to kill (die; or exit; or wp_die()) the whole script you aren't going to be able to stop the comment posting. I tested by hooking return false; to the filter. Nothing.

You can kill comment submission by hooking return false; to pre_comment_approved though, and you do have the IP data in the second parameter.

add_filter('pre_comment_approved', 'limit_commentator', 1, 2);
function limit_commentator( $approved, $commentdata ) {

You can check the $wpdb->comments table for that IP (in the comment_author_IP column) and return false if you get a match. It should be easy, but I wouldn't do it. Many residential internet connections share the same external IP. If you limit by IP you may be limiting comments to one per neighborhood. Plus, residential IPs change. This is not reliable.

Rate limiting (per day / per hour) based on IP is a much better idea only because the odds are in your favor, and the implementation would be pretty similar.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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