0

This is the function I use to only allow comments longer than 60 chars:

add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );
function wpb_preprocess_comment($comment) {
if ( strlen( $comment['comment_content'] ) < 60 ) {
    wp_die('Minimum allowed comment length is 60 characters.');
}
return $comment;
}

How do I make this filter apply to comments only on a specific page, say, is_page(42)?

1 Answer 1

3

is_page() is not available at that point try using global $post that way the post ID should be available for comparison.

add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );
function wpb_preprocess_comment($comment) {
global $post;
if ((strlen( $comment['comment_content'] ) < 60 ) && ($post->ID == 42)) {
    wp_die('Minimum allowed comment length is 60 characters.');
}
return $comment;
}
4
  • No, it doesn't work, unfortunately.
    – pereyra
    Commented May 28, 2015 at 15:20
  • Looks like is_page is not available at that point. Look at this post -> wordpress.stackexchange.com/questions/21418/…
    – gdaniel
    Commented May 28, 2015 at 15:32
  • I don't really need to necessarily use is_page(). Any other workable way of specifying post id within the function should be sufficient.
    – pereyra
    Commented May 28, 2015 at 15:47
  • Updated the answer.
    – gdaniel
    Commented May 28, 2015 at 16:05

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.