0

I want to create few comments custom fields for my CPT and remove the default ones.

I will have just two custom fields more exactly 2 radio inputs.

Here is my code:

function debate_comment_fields( $fields ) {

 if( is_singular( 'debate' ) ) {

     $fields['url'] = ''; 
    $fields['author'] = ''; 
    $fields['email'] = '';


    $fields['first'] = '<p class="comment-form-first"><label for="first">' . 
                     __( 'HTML5' ) . 
                     '</label>' . 
                     '<input id="first" name="category" type="radio" value="'.get_post_meta($post->ID, 'agree', true).'" /></p>';


    $fields['second'] = '<p class="comment-form-second"><label for="second">' . 
                       __( 'FLASH' ) . 
                       '</label>' .
                      '<input id="second" name="category" type="radio" value="'.get_post_meta($post->ID, 'disagree', true).'" /></p>';

    return $fields;


}
} 
add_filter('comment_form_default_fields','debate_comment_fields');  

The problem is that on my default blog posts the url, author and email fields are removed also and I'm expecting to see them there because I've added if( is_singular( 'debate' ) ) {

And if I submit a comment inside my CPT I get: ERROR: please fill the required fields (name, email). It seems that it's still asking me for those fields.

What's wrong with my code? Thanks!

1 Answer 1

1

Non-CPT Comments

Regarding the first issue: you're not returning anything if your conditional is false. To simplify your code:

function debate_comment_fields( $fields ) {
    // If conditional is true
    if( is_singular( 'debate' ) ) {

        // Do some stuff to $fields
        // Return $fields
        return $fields;
    }
    // If conditional is not true?
} 
add_filter('comment_form_default_fields','debate_comment_fields');  

You need to return the default $fields if your conditional is not true:

function debate_comment_fields( $fields ) {
    // If conditional is true
    if( is_singular( 'debate' ) ) {

        // Do some stuff to $fields
        // Return $fields
        return $fields;
    }
    // If conditional is not true?
    return $fields;
} 
add_filter('comment_form_default_fields','debate_comment_fields');  

This will ensure that the default fields are returned for non-CPT post comments.

CPT Comments

The second issue you're running into is that, for your CPT comments, the comment form isn't sending name and email, which are required. There are two solutions:

  1. Disable name/email as required fields globally
  2. Filter preprocess_comment to allow CPT post comments to bypass the required-field check
1
  • Regarding the first issue, it's working, THANK YOU!
    – agis
    Commented Nov 12, 2013 at 23:58

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.