最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

comment form - comment_form() Not changing the default classid of elements

programmeradmin2浏览0评论

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

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

Share Improve this question edited May 4, 2017 at 20:25 Frederick M. Rogers asked May 4, 2017 at 17:54 Frederick M. RogersFrederick M. Rogers 1352 silver badges10 bronze badges 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(). – Dave Romsey Commented 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? – Frederick M. Rogers Commented 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. – Dave Romsey Commented May 5, 2017 at 18:58
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论