2

I am building a custom WordPress theme and I'm trying to tweak the comments form. If you go to my blog page and scroll down to the comments section (bottom of each entry) you'll notice that the comments section has two textarea fields. I don't want the first one, but I'm not sure why or how it's getting inserted in there. I'd like the order of the form to just be: name, email, comment. The code for the extra text area is not in the code I've written for the custom comment form in functions.php:

function alpha_comments_defaults($defaults){
    $defaults['id_form'] = '';
    $defaults['id_submit'] = '';

    return $defaults;
}

function alpha_comments_fields(){
    $commenter= wp_get_current_commenter();
    $req = get_option('require_name_email');
    $aria_req = ($req ? " aria-required='true'" : ' ');

    $fields =  array(

        'author' =>
            '<p class="comment-form-author"><label>' . __( 'Name', 'domainreference' ) . ( $req ? '<span class="required">*</span>' : '' ) . '</label> ' .
            '<input name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" ' . $aria_req . ' /></p>',

        'email' =>
            '<p class="comment-form-email"><label>' . __( 'Email', 'domainreference' ) .  ( $req ? '<span class="required">*</span>' : '' ) . '</label> ' .
            '<input name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" ' . $aria_req . ' /></p>',

        'url' =>
            '',

        'comment_field' =>
            '<p class="comment-form-comment"><label>' . _x( 'Comment', 'noun' ) . '</label>' .
            '<textarea name="comment" cols="45" rows="8" ' . $aria_req . '></textarea></p>'
    );

    return $fields;
}

add_filter('comment_form_defaults', 'alpha_comments_defaults');
add_filter('comment_form_default_fields', 'alpha_comments_fields');

How do I get rid of the extra form field?

1 Answer 1

3

It seems that WordPress handles the comment field separately than the other fields. If you look at comment_form() in wp-includes/comment-template.php, you can see this.

It's possible to set $defaults['comment_field'] to false in alpha_comments_defaults() then add the comment field markup to $fields['comment_field'] in alpha_comments_fields() in the desired order, but this could cause trouble with plugins.

I've moved things around and added code to handle the field ordering that you requested.

function alpha_comments_defaults( $defaults ) {
    $defaults['id_form'] = '';
    $defaults['id_submit'] = '';
    $defaults['comment_field'] = '<p class="comment-form-comment"><label>' . _x( 'Comment', 'noun' ) . '</label>' .
                                                                '<textarea name="comment" cols="45" rows="8" aria-required="true"></textarea></p>';
    return $defaults;
}
add_filter('comment_form_defaults', 'alpha_comments_defaults');


function alpha_comments_fields( $fields ) {
    $commenter= wp_get_current_commenter();
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : ' ' );

    $fields = array(
                        'author' =>
                                '<p class="comment-form-author"><label>' . __( 'Name', 'domainreference' ) . ( $req ? '<span class="required">*</span>' : '' ) . '</label> ' .
                                '<input name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" ' . $aria_req . ' /></p>',

                        'email' =>
                                '<p class="comment-form-email"><label>' . __( 'Email', 'domainreference' ) .  ( $req ? '<span class="required">*</span>' : '' ) . '</label> ' .
                                '<input name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" ' . $aria_req . ' /></p>',

                        'url' => '',
    );

    return $fields;
}
add_filter('comment_form_default_fields', 'alpha_comments_fields');


// Reorder comment fields.
// http://wordpress.stackexchange.com/a/218324/2807
function alpha_move_comment_field( $fields ) {
    $comment_field = $fields['comment'];
    unset( $fields['comment'] );
    $fields['comment'] = $comment_field;

    return $fields;
}
add_filter( 'comment_form_fields', 'alpha_move_comment_field' );
0

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.