Skip to content

Instantly share code, notes, and snippets.

@Trippnology
Created October 12, 2015 13:36
Show Gist options
  • Save Trippnology/877538c6ab7bb0f06d21 to your computer and use it in GitHub Desktop.
Save Trippnology/877538c6ab7bb0f06d21 to your computer and use it in GitHub Desktop.
WP: Add Bootstrap classes to comment form
// Add to your theme's comments.php
<?php
$comment_args = array(
'class_submit' => 'btn btn-default 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>',
)
);
comment_form($comment_args);
?>
@lordmatt
Copy link

lordmatt commented Jun 27, 2019

Quick question. Where are you getting the variables from? With strict reporting PHP throws an absolute fit at all those uninitialised variables.

@lordmatt
Copy link

I figured it out.

    $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']='';
    }

@Trippnology
Copy link
Author

Yeah sorry, this wasn't very well documented, and is probably well out of date by now. Thanks for coming back and adding your findings.

@lordmatt
Copy link

lordmatt commented Jul 1, 2019

No problem. It was exactly what I needed. (Once I figured out the missing bit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment