From e4cf59e251e4db07d8c4d67119e4813416ea6582 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Sat, 18 Apr 2020 20:55:25 +0200 Subject: [PATCH] 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. --- inc/custom-comments.php | 107 ++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 43 deletions(-) diff --git a/inc/custom-comments.php b/inc/custom-comments.php index cf38f26..6292a74 100644 --- a/inc/custom-comments.php +++ b/inc/custom-comments.php @@ -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' ); -/** - * Creates the comments form. - * - * @param string $fields Form fields. - * - * @return array - */ - if ( ! function_exists( 'understrap_bootstrap_comment_form_fields' ) ) { - + /** + * Add Bootstrap classes to WP's comment form default 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 + */ 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' => '
' . - '
', - 'email' => '
' . - '
', - 'url' => '
' . - '
', - 'cookies' => '', + + $replace = array( + '

'' => '', ); + 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( + '

' '

- - -
'; - $args['class_submit'] = 'btn btn-secondary'; // since WP 4.1. + $replace = array( + '

'