Skip to content

Instantly share code, notes, and snippets.

@feryardiant
Created October 24, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feryardiant/5eb47ca8572190eb82acf31fd58d1d0d to your computer and use it in GitHub Desktop.
Save feryardiant/5eb47ca8572190eb82acf31fd58d1d0d to your computer and use it in GitHub Desktop.
Twitter Bootstrap Comment Form for Wordpress
<?php
if ( ! function_exists( 'twbs_comment_form_defaults' ) ) {
/**
* Customize default wp comment form.
*
* @link https://developer.wordpress.org/reference/hooks/comment_form_defaults/
* @link https://codex.wordpress.org/Function_Reference/comment_form
*
* @param array $defaults
* @return array
*/
function twbs_comment_form_defaults( $defaults ) {
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$labels = [
'author' => __( 'Name' ),
'email' => __( 'Email' ),
'url' => __( 'Website' ),
'comment' => _x( 'Comment', 'noun' ),
'required' => __( 'required' )
];
$req_html = sprintf( '(%s)', $labels['required'] );
$req_attr = $req ? 'aria-required="true" required' : '';
$req_mark = $req ? ' ' . $req_html : '';
$defaults['submit_field'] = '<div class="form-group form-submit">%1$s %2$s</div>';
$defaults['class_submit'] = 'btn btn-primary submit';
$defaults['comment_field'] = implode( '', [
'<div class="form-group comment-form-comment">',
'<label for="comment" class="sr-only">' . $labels['comment'] . $req_html . '</label>',
sprintf(
'<textarea id="comment" name="comment" class="form-control" rows="8" placeholder="%1$s" aria-required="true" required="required"></textarea>',
$labels['comment'] . ' *'
),
'</div>'
] );
$defaults['fields'] = [
'author' => implode( '', [
'<div class="form-group comment-form-author">',
'<label for="author" class="sr-only">' . $labels['author'] . $req_mark . '</label> ',
sprintf(
'<input id="author" name="author" class="form-control" type="text" value="%1$s" placeholder="%2$s" maxlength="245" %3$s />',
esc_attr( $commenter['comment_author'] ),
$labels['author'] . ( $req ? ' *' : '' ),
$req_attr
),
'</div>'
] ),
'email' => implode( '', [
'<div class="form-group comment-form-email">',
'<label for="email" class="sr-only">' . $labels['email'] . $req_mark . '</label> ',
sprintf(
'<input id="email" name="email" class="form-control" type="email" value="%1$s" placeholder="%2$s" maxlength="100" aria-describedby="email-notes" %3$s />',
esc_attr( $commenter['comment_author_email'] ),
$labels['email'] . ( $req ? ' *' : '' ),
$req_attr
),
'</div>'
] ),
'url' => implode( '', [
'<div class="form-group comment-form-url">',
'<label for="url" class="sr-only">' . $labels['url'] . '</label> ',
sprintf(
'<input id="url" name="url" class="form-control" type="url" value="%1$s" placeholder="%2$s" size="30" maxlength="200" />',
esc_attr( $commenter['comment_author_url'] ),
$labels['url']
),
'</div>'
] ),
];
return $defaults;
}
add_filter( 'comment_form_defaults', 'twbs_comment_form_defaults' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment