0

I have a comment form that has name, email, brief, detailed and rating fields.

The name, email, brief and rating need to be required, but the latter two are custom fields and I can only make them required through Javascript, but want to add proper WordPress/PHP validation to those fields.

The detailed field is actually the comment_field which is currently required, but I want to removed the required for this field, but I don't want to do it via the WordPress settings as I still want name and email to be required.

1 Answer 1

2

Here is a related question that should answer yours. Basicly you want to use the pre_comment_on_post hook

    function custom_validate_comment() {
    //validate for brief and rating
if( empty( $_POST['brief']) || empty( $_POST['rating'])  )
        wp_die( __('Error: you must fill in both the rating and the brief') );
//make comment not required
if(empy($_POST['comment']){$_POST['comment'] == "empty_comment";}
}

add_action('pre_comment_on_post', 'custom_validate_comment');

function custom_change_comment( $commentdata ) {
    if( $commentdata['comment'] == 'empty_comment' )
        $commentdata['comment'] = '';
    return $commentdata;
}

add_filter('preprocess_comment', 'custom_change_comment');

That should at least get you on the right track

1
  • Awesome, that worked perfectly. Thanks for the help!
    – WolfieZero
    Oct 18, 2011 at 9:42

Your Answer

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

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