How do I check which comment fields are required in my theme?

I plan to use comment_form() with a bunch of arguments to format the output. However, I need to know which fields have b

I plan to use comment_form() with a bunch of arguments to format the output. However, I need to know which fields have been set to required (to style them as such). How do I do that?

I plan to use comment_form() with a bunch of arguments to format the output. However, I need to know which fields have been set to required (to style them as such). How do I do that?

Share Improve this question asked Jun 27, 2019 at 4:56 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges 2
  • 1 The default output of comment_form() marks required fields with a * – Jacob Peattie Commented Jun 27, 2019 at 5:23
  • True, but I'm messing about with that. Thus I need to know what to put back. – Matthew Brown aka Lord Matt Commented Jun 27, 2019 at 5:31
Add a comment  | 

1 Answer 1

Reset to default 0

After some digging in the core code (as the filters in this area appear to be mostly undocumented), I discovered that this is how WP does it.

$req = get_option( 'require_name_email' );

From there it is trivial to work out how to reformat the comment form.

    $html5         = TRUE; // False if xhtml
    $req           = get_option( 'require_name_email' );
    $html_req      = ( $req ? " required='required'" : '' );
    $commenter     = wp_get_current_commenter();
    $aria_req      = ( $req ? " aria-required='true'" : '' );

    if(!isset($commenter['comment_author'])){
        $commenter['comment_author']='';
    }
    if(!isset($commenter['comment_author_email'])){
        $commenter['comment_author_email']='';
    }
    if(!isset($commenter['comment_author_url'])){
        $commenter['comment_author_url']='';
    }

    $comment_args = array(
    'class_submit' => 'btn btn-outline-primary submit',
    'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" class="form-control" cols="45" rows="8" aria-required="true" required="required"></textarea></p>',
    'fields' => array(
        'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
        '<input id="author" name="author" class="form-control" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . $html_req . ' /></p>',
        'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
        '<input id="email" name="email" class="form-control" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" aria-describedby="email-notes"' . $aria_req . $html_req  . ' /></p>',
        'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
        '<input id="url" name="url" class="form-control" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
        ),
    'format'=>'html5'
);
comment_form($comment_args);

If you are working with Bootstrap, your form now looks lovely.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745366323a4624597.html

相关推荐

  • How do I check which comment fields are required in my theme?

    I plan to use comment_form() with a bunch of arguments to format the output. However, I need to know which fields have b

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信