1

Is it possible to cancel comment submission if certain creteria are met?

I want to limit the number of characters in a comment. Where do I test the length and cancel the comment if it's over a given length?

Would the pre_comment_approved filter do this? And does anyone have some sample code for how to use pre_comment_approved ?

Thanks

1 Answer 1

0

You are certainly on the right track. You can create a plugin (create a directory in "wp-content/plugins" and create a php file with the same name as the folder) with the following code:

function wpse_33944_filter_handler( $approved , $commentdata ){
    if(strlen($commentdata[comment_content]) > 5) {
        return false;
    }
    return true;
}
add_filter( 'pre_comment_approved' , 'wpse_33944_filter_handler' , '99', 2 );
2
  • 1
    Thanks for that. Do I need to make it a plugin? Or can I chuck the code in functions.php in the theme I'm working on?
    – nedlud
    Commented Nov 17, 2011 at 4:58
  • You can probably chuck it in functions.php. But creating it as a plugin seems cleaner :)
    – GavinR
    Commented Nov 17, 2011 at 14:33

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.