Skip to content

Commit 99705b3

Browse files
committed
Comments: Mark comment text field as required.
Add required asterisk to the comment text field. Historically, the name and email fields are marked as required, but the comment text field is not, though it is actually a required field. Props infected, solarissmoke, rianrietveld, afercia, sabernhardt, strider72, mai21, audrasjb. Fixes #16206. git-svn-id: https://develop.svn.wordpress.org/trunk@52029 602fd350-edb4-49c9-b593-d223f7449a82
1 parent cdb15e3 commit 99705b3

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

src/wp-includes/comment-template.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,36 +2350,42 @@ function comment_form( $args = array(), $post_id = null ) {
23502350
$args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
23512351
}
23522352

2353-
$req = get_option( 'require_name_email' );
2354-
$html_req = ( $req ? " required='required'" : '' );
2355-
$html5 = 'html5' === $args['format'];
2353+
$req = get_option( 'require_name_email' );
2354+
$html5 = 'html5' === $args['format'];
2355+
2356+
// Define attributes in HTML5 or XHTML syntax.
2357+
$required_attribute = ( $html5 ? ' required' : ' required="required"' );
2358+
$checked_attribute = ( $html5 ? ' checked' : ' checked="checked"' );
2359+
2360+
// Identify required fields visually.
2361+
$required_indicator = ' <span class="required" aria-hidden="true">*</span>';
23562362

23572363
$fields = array(
23582364
'author' => sprintf(
23592365
'<p class="comment-form-author">%s %s</p>',
23602366
sprintf(
23612367
'<label for="author">%s%s</label>',
23622368
__( 'Name' ),
2363-
( $req ? ' <span class="required">*</span>' : '' )
2369+
( $req ? $required_indicator : '' )
23642370
),
23652371
sprintf(
23662372
'<input id="author" name="author" type="text" value="%s" size="30" maxlength="245"%s />',
23672373
esc_attr( $commenter['comment_author'] ),
2368-
$html_req
2374+
( $req ? $required_attribute : '' )
23692375
)
23702376
),
23712377
'email' => sprintf(
23722378
'<p class="comment-form-email">%s %s</p>',
23732379
sprintf(
23742380
'<label for="email">%s%s</label>',
23752381
__( 'Email' ),
2376-
( $req ? ' <span class="required">*</span>' : '' )
2382+
( $req ? $required_indicator : '' )
23772383
),
23782384
sprintf(
23792385
'<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes"%s />',
23802386
( $html5 ? 'type="email"' : 'type="text"' ),
23812387
esc_attr( $commenter['comment_author_email'] ),
2382-
$html_req
2388+
( $req ? $required_attribute : '' )
23832389
)
23842390
),
23852391
'url' => sprintf(
@@ -2397,7 +2403,7 @@ function comment_form( $args = array(), $post_id = null ) {
23972403
);
23982404

23992405
if ( has_action( 'set_comment_cookies', 'wp_set_comment_cookies' ) && get_option( 'show_comments_cookies_opt_in' ) ) {
2400-
$consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
2406+
$consent = empty( $commenter['comment_author_email'] ) ? '' : $checked_attribute;
24012407

24022408
$fields['cookies'] = sprintf(
24032409
'<p class="comment-form-cookies-consent">%s %s</p>',
@@ -2419,8 +2425,8 @@ function comment_form( $args = array(), $post_id = null ) {
24192425

24202426
$required_text = sprintf(
24212427
/* translators: %s: Asterisk symbol (*). */
2422-
' ' . __( 'Required fields are marked %s' ),
2423-
'<span class="required">*</span>'
2428+
' <span class="comment-required-message" aria-hidden="true">' . __( 'Required fields are marked %s' ) . '</span>',
2429+
trim( $required_indicator )
24242430
);
24252431

24262432
/**
@@ -2437,10 +2443,11 @@ function comment_form( $args = array(), $post_id = null ) {
24372443
'comment_field' => sprintf(
24382444
'<p class="comment-form-comment">%s %s</p>',
24392445
sprintf(
2440-
'<label for="comment">%s</label>',
2441-
_x( 'Comment', 'noun' )
2446+
'<label for="comment">%s%s</label>',
2447+
_x( 'Comment', 'noun' ),
2448+
$required_indicator
24422449
),
2443-
'<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
2450+
'<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525"' . $required_attribute . '></textarea>'
24442451
),
24452452
'must_log_in' => sprintf(
24462453
'<p class="must-log-in">%s</p>',
@@ -2452,7 +2459,7 @@ function comment_form( $args = array(), $post_id = null ) {
24522459
)
24532460
),
24542461
'logged_in_as' => sprintf(
2455-
'<p class="logged-in-as">%s</p>',
2462+
'<p class="logged-in-as">%s%s</p>',
24562463
sprintf(
24572464
/* translators: 1: Edit user link, 2: Accessibility text, 3: User name, 4: Logout URL. */
24582465
__( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
@@ -2462,15 +2469,16 @@ function comment_form( $args = array(), $post_id = null ) {
24622469
$user_identity,
24632470
/** This filter is documented in wp-includes/link-template.php */
24642471
wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
2465-
)
2472+
),
2473+
$required_text
24662474
),
24672475
'comment_notes_before' => sprintf(
24682476
'<p class="comment-notes">%s%s</p>',
24692477
sprintf(
24702478
'<span id="email-notes">%s</span>',
24712479
__( 'Your email address will not be published.' )
24722480
),
2473-
( $req ? $required_text : '' )
2481+
$required_text
24742482
),
24752483
'comment_notes_after' => '',
24762484
'action' => site_url( '/wp-comments-post.php' ),

src/wp-includes/comment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3549,7 +3549,7 @@ function wp_handle_comment_submission( $comment_data ) {
35493549

35503550
if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
35513551
if ( '' == $comment_author_email || '' == $comment_author ) {
3552-
return new WP_Error( 'require_name_email', __( '<strong>Error</strong>: Please fill the required fields (name, email).' ), 200 );
3552+
return new WP_Error( 'require_name_email', __( '<strong>Error</strong>: Please fill the required fields.' ), 200 );
35533553
} elseif ( ! is_email( $comment_author_email ) ) {
35543554
return new WP_Error( 'require_valid_email', __( '<strong>Error</strong>: Please enter a valid email address.' ), 200 );
35553555
}

0 commit comments

Comments
 (0)