1

EDIT I am trying to change the class and id of the <form> element and well as the <input>/Submit button field using the comment_form() function. How ever it appears that various string values are being echoed out instead of replacing the default values (see demo link).

CODE:

<?php if ( comments_open() ) : ?>

    <?php
        $req = get_option( 'require_name_email' );
        $aria_req = ( $req ? ' aria-required="true"' : '' );

        $fields = array(
            'id_form'       => 'comment-form',
            'class_form'    => 'form-inline',
            'class_submit'  => 'btn btn-default',
            'author'        => '<div class="form-group">
                                <label for="comment-author" class="sr-only">' . __( 'Author', 'magneton' ) . '</label>
                                <input type="text" name="author" id="comment-author" class="form-control" placeholder="' . __( 'Author (required)', 'magneton' ) . '"' . $aria_req . '>',
            'email'         => '<label for="comment-author-email" class="sr-only">' . __( 'E-Mail', 'magneton' ) . '</label>
                                <input type="email" name="email" id="comment-author-email" class="form-control" placeholder="' . __( 'E-Mail (required)', 'magneton' ) . '"' . $aria_req . '>',
            'url'            => '<label for="comment-author-url" class="sr-only">' . __( 'Website', 'magneton' ) . '</label>
                                <input type="url" name="url" id="comment-author-url" class="form-control" placeholder="' . __( 'Website'. 'magneton' ) . '">
                                </div>'
        );

        $comments_args = array(
            'fields' => $fields,
            'title_reply' => __( 'Leave a reply', 'magneton' ),
            'comment_field' => '<textarea name="comment" id="comment" class="form-control"  aria-required="true" rows="10"></textarea>',
            'label_submit' => __( 'Submit Comment', 'magneton' )
        );
    ?>

    <?php comment_form( $comments_args ); ?> 

<?php endif; ?>

LINK: DEMO

3
  • 1
    Move the id_form, class_form, and class_submit items out of $fields and into the $comments_args array (alongside title_reply, comment_field, etc). See comment_form(). May 4, 2017 at 20:33
  • 1
    @DaveRomsey, thank i looked over the documentation (again) and you are correct. I swear sometime i get so blind looking at code. You should make this an answer so i can check it off. Also one last question, is there any way to re-arrange the order of the fields? May 5, 2017 at 17:21
  • Thank you! I've added an answer and linked to another solution that covers rearranging and customizing the comment form fields. May 5, 2017 at 18:58

1 Answer 1

1

Those classes are being rendered inline because the id_form, class_form, and class_submit parameters should be placed within the $comments_args array (alongside title_reply, comment_field, etc) and not within the $fields array. See comment_form().

Also one last question, is there any way to re-arrange the order of the fields?

Yes, the fields can be reordered. Here's an answer that shows how to accomplish reordering and customizing the comment fields. The main gotcha with reordering the comment fields is that if you want the main comment field to appear above the name/email/URL/etc fields, the comment_form_defaults filter needs to be used to set $comment_field to an empty string. This is because of how the parameters are set up; $comment_field is special and not part of the $fields array by default. This is covered in the answer linked above.

8
  • Thanks, i really appreciate the time and effort you put into this. May 5, 2017 at 19:17
  • I hate to bother you again, but just as i was all happy that code worked as presented in your link, i come to realize none of the custom form fields are visible when i am logged in to my wordpress installation. Any idea ? May 6, 2017 at 18:11
  • No worries! That's a good question. Here's an answer that covers that, and more. May 6, 2017 at 19:18
  • 1
    Thanks, i will have read through the various links i am sure i will find the answere! May 6, 2017 at 19:29
  • 1
    No worries, i appreciate the link, it helped me realize that the fields i was using where in fact native to WordPress and that my own solution would in fact work. So again, thank you. May 6, 2017 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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