WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?


share|improve this question
up vote 0 down vote accepted

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 );
share|improve this answer
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 Nov 17 '11 at 4:58
You can probably chuck it in functions.php. But creating it as a plugin seems cleaner :) – GavinR Nov 17 '11 at 14:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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