Use str_replace to add Bootstrap classes
Advantages: - Uses WP's default field HTML which has changed and differs from the version in this file. - Uses WP's default translations. No need to have the strings in the theme language files anymore.
This commit is contained in:
parent
064e1d4534
commit
e4cf59e251
|
@ -8,61 +8,82 @@
|
|||
// Exit if accessed directly.
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
// Comments form.
|
||||
// Add Bootstrap classes to comment form fields.
|
||||
add_filter( 'comment_form_default_fields', 'understrap_bootstrap_comment_form_fields' );
|
||||
|
||||
if ( ! function_exists( 'understrap_bootstrap_comment_form_fields' ) ) {
|
||||
/**
|
||||
* Creates the comments form.
|
||||
* Add Bootstrap classes to WP's comment form default fields.
|
||||
*
|
||||
* @param string $fields Form fields.
|
||||
* @param array $fields {
|
||||
* Default comment fields.
|
||||
*
|
||||
* @type string $author Comment author field HTML.
|
||||
* @type string $email Comment author email field HTML.
|
||||
* @type string $url Comment author URL field HTML.
|
||||
* @type string $cookies Comment cookie opt-in field HTML.
|
||||
* }
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
if ( ! function_exists( 'understrap_bootstrap_comment_form_fields' ) ) {
|
||||
|
||||
function understrap_bootstrap_comment_form_fields( $fields ) {
|
||||
$commenter = wp_get_current_commenter();
|
||||
$req = get_option( 'require_name_email' );
|
||||
$aria_req = ( $req ? " aria-required='true'" : '' );
|
||||
$html5 = current_theme_supports( 'html5', 'comment-form' ) ? 1 : 0;
|
||||
$consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
|
||||
$fields = array(
|
||||
'author' => '<div class="form-group comment-form-author"><label for="author">' . __( 'Name',
|
||||
'understrap' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
|
||||
'<input class="form-control" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . '></div>',
|
||||
'email' => '<div class="form-group comment-form-email"><label for="email">' . __( 'Email',
|
||||
'understrap' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
|
||||
'<input class="form-control" id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . '></div>',
|
||||
'url' => '<div class="form-group comment-form-url"><label for="url">' . __( 'Website',
|
||||
'understrap' ) . '</label> ' .
|
||||
'<input class="form-control" id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30"></div>',
|
||||
'cookies' => '<div class="form-group form-check comment-form-cookies-consent"><input class="form-check-input" id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' /> ' .
|
||||
'<label class="form-check-label" for="wp-comment-cookies-consent">' . __( 'Save my name, email, and website in this browser for the next time I comment', 'understrap' ) . '</label></div>',
|
||||
|
||||
$replace = array(
|
||||
'<p class="' => '<div class="form-group ',
|
||||
'<input' => '<input class="form-control" ',
|
||||
'</p>' => '</div>',
|
||||
);
|
||||
|
||||
if ( isset( $fields['author'] ) ) {
|
||||
$fields['author'] = strtr( $fields['author'], $replace );
|
||||
}
|
||||
if ( isset( $fields['email'] ) ) {
|
||||
$fields['email'] = strtr( $fields['email'], $replace );
|
||||
}
|
||||
if ( isset( $fields['url'] ) ) {
|
||||
$fields['url'] = strtr( $fields['url'], $replace );
|
||||
}
|
||||
|
||||
$replace = array(
|
||||
'<p class="' => '<div class="form-group form-check ',
|
||||
'<input' => '<input class="form-check-input" ',
|
||||
'<label' => '<label class="form-check-label" ',
|
||||
'</p>' => '</div>',
|
||||
);
|
||||
if ( isset( $fields['cookies'] ) ) {
|
||||
$fields['cookies'] = strtr( $fields['cookies'], $replace );
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
} // endif function_exists( 'understrap_bootstrap_comment_form_fields' )
|
||||
} // End of if function_exists( 'understrap_bootstrap_comment_form_fields' )
|
||||
|
||||
// Add Bootstrap classes to comment form submit button and comment field.
|
||||
add_filter( 'comment_form_defaults', 'understrap_bootstrap_comment_form' );
|
||||
|
||||
if ( ! function_exists( 'understrap_bootstrap_comment_form' ) ) {
|
||||
/**
|
||||
* Builds the form.
|
||||
*
|
||||
* @param string $args Arguments for form's fields.
|
||||
* @param string[] $args Comment form arguments and fields.
|
||||
*
|
||||
* @return mixed
|
||||
* @return string[]
|
||||
*/
|
||||
|
||||
if ( ! function_exists( 'understrap_bootstrap_comment_form' ) ) {
|
||||
|
||||
function understrap_bootstrap_comment_form( $args ) {
|
||||
$args['comment_field'] = '<div class="form-group comment-form-comment">
|
||||
<label for="comment">' . _x( 'Comment', 'noun', 'understrap' ) . ( ' <span class="required">*</span>' ) . '</label>
|
||||
<textarea class="form-control" id="comment" name="comment" aria-required="true" cols="45" rows="8"></textarea>
|
||||
</div>';
|
||||
$args['class_submit'] = 'btn btn-secondary'; // since WP 4.1.
|
||||
$replace = array(
|
||||
'<p class="' => '<div class="form-group ',
|
||||
'<textarea' => '<textarea class="form-control" ',
|
||||
'</p>' => '</div>',
|
||||
);
|
||||
|
||||
if ( isset( $args['comment_field'] ) ) {
|
||||
$args['comment_field'] = strtr( $args['comment_field'], $replace );
|
||||
}
|
||||
|
||||
if ( isset( $args['class_submit'] ) ) {
|
||||
$args['class_submit'] = 'btn btn-secondary';
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
} // endif function_exists( 'understrap_bootstrap_comment_form' )
|
||||
} // End of if function_exists( 'understrap_bootstrap_comment_form' ).
|
||||
|
|
Reference in New Issue