Skip to content

Commit 23a215a

Browse files
committed
Accessibility: Add invalid password message for post passwords.
Display a message notifying the user of an incorrect password when submitting the post password form. Improve the accessibility of the form by adding a required attribute for consistent identification. Props henry.wright, jonnyauk, kreppar, tommusrhodus, joedolson, audrasjb, jdahir0789, parthvataliya, dhruvang21. Fixes #37332. git-svn-id: https://develop.svn.wordpress.org/trunk@59736 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 01e553b commit 23a215a

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/wp-includes/post-template.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,11 +1774,33 @@ function prepend_attachment( $content ) {
17741774
* @return string HTML content for password form for password protected post.
17751775
*/
17761776
function get_the_password_form( $post = 0 ) {
1777-
$post = get_post( $post );
1778-
$label = 'pwbox-' . ( empty( $post->ID ) ? rand() : $post->ID );
1779-
$output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
1777+
$post = get_post( $post );
1778+
$field_id = 'pwbox-' . ( empty( $post->ID ) ? wp_rand() : $post->ID );
1779+
$invalid_password = '';
1780+
$invalid_password_html = '';
1781+
$aria = '';
1782+
$class = '';
1783+
1784+
// If the referrer is the same as the current request, the user has entered an invalid password.
1785+
if ( ! empty( $post->ID ) && wp_get_raw_referer() === get_permalink( $post->ID ) && isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
1786+
/**
1787+
* Filters the invalid password message shown on password-protected posts.
1788+
* The filter is only applied if the post is password protected.
1789+
*
1790+
* @since 6.8.0
1791+
*
1792+
* @param string The message shown to users when entering an invalid password.
1793+
* @param WP_Post $post Post object.
1794+
*/
1795+
$invalid_password = apply_filters( 'the_password_form_incorrect_password', __( 'Invalid password.' ), $post );
1796+
$invalid_password_html = '<div class="post-password-form-invalid-password" role="alert"><p id="error-' . $field_id . '">' . $invalid_password . '</p></div>';
1797+
$aria = ' aria-describedby="error-' . $field_id . '"';
1798+
$class = ' password-form-error';
1799+
}
1800+
1801+
$output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form' . $class . '" method="post">' . $invalid_password_html . '
17801802
<p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
1781-
<p><label for="' . $label . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $label . '" type="password" spellcheck="false" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
1803+
<p><label for="' . $field_id . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $field_id . '" type="password" spellcheck="false" required size="20"' . $aria . ' /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
17821804
';
17831805

17841806
/**
@@ -1791,11 +1813,13 @@ function get_the_password_form( $post = 0 ) {
17911813
*
17921814
* @since 2.7.0
17931815
* @since 5.8.0 Added the `$post` parameter.
1816+
* @since 6.8.0 Added the `$invalid_password` parameter.
17941817
*
17951818
* @param string $output The password form HTML output.
17961819
* @param WP_Post $post Post object.
1820+
* @param string $invalid_password The invalid password message.
17971821
*/
1798-
return apply_filters( 'the_password_form', $output, $post );
1822+
return apply_filters( 'the_password_form', $output, $post, $invalid_password );
17991823
}
18001824

18011825
/**

0 commit comments

Comments
 (0)