Opened 20 months ago

Last modified 20 months ago

#18729 new feature request

No HTML Classes/IDs Associated With get_the_password_form() Output

Reported by: philiparthurmoore Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version: 3.2.1
Severity: normal Keywords: westi-likes has-patch
Cc:

Description

Currently get_the_password_form() outputs a password form that offers no HTML classes or IDs for proper styling:

/**
 * Retrieve protected post password form content.
 *
 * @since 1.0.0
 * @uses apply_filters() Calls 'the_password_form' filter on output.
 *
 * @return string HTML content for password form for password protected post.
 */
function get_the_password_form() {
	global $post;
	$label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
	$output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
	<p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
	<p><label for="' . $label . '">' . __("Password:") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>
	</form>
	';
	return apply_filters('the_password_form', $output);
}

It would be quite useful to add classes to the form and input elements in this output so that we can properly style the password form without having to filter the output in, for example, a theme's functions.php file.

Attachments (2)

get_the_password_form.patch (1.4 KB) - added by philiparthurmoore 20 months ago.
Adjusted spacing in patch and added semantic classes to form
get_the_password_form-azaozz.patch (1.3 KB) - added by philiparthurmoore 20 months ago.
Taking into account Andrew Ozz's suggestions

Download all attachments as: .zip

Change History (10)

If a theme uses post_class(), it should be possible to style the form using
.post-password-required form.

comment:2 in reply to: ↑ 1   westi20 months ago

  • Keywords needs-patch westi-likes added

Replying to SergeyBiryukov:

If a theme uses post_class(), it should be possible to style the form using
.post-password-required form.

True.

It would still be nice to add a class on the form anyway.

comment:3 in reply to: ↑ 1   philiparthurmoore20 months ago

Replying to SergeyBiryukov:

If a theme uses post_class(), it should be possible to style the form using
.post-password-required form.

Yes, but on index.php and archive.php-generated pages that have multiple password-protected posts it would be useful to have more fine-grained control over the forms. Also, there may be other forms/input on password protected pages/posts that may be adversely affected by using the body class to style those inputs. Cheers.

Edit. I obviously didn't read this correctly. Disregard my comments about body class. I think they may still apply to post class, though.

Last edited 20 months ago by philiparthurmoore (previous) (diff)

@philiparthurmoore: some feedback on your patch:

  • Thanks for updating the spacing as you went through :)
  • I would name the classes: post-password-form, post-password-input, and post-password-submit - that way they are clearly semantic.

If you could update your patch to make those couple of changes that would be great :)

Adjusted spacing in patch and added semantic classes to form

Thanks very much for your feedback, @westi. I've added those changes to the patch and re-attached it. Cheers.

  • Keywords has-patch added; needs-patch removed

comment:7 follow-up: ↓ 8   azaozz20 months ago

Looking at get_the_password_form.patch we won't need 3 classes to style 3 elements. A post-password-form class on the <form> is enough. Also we don't need a randomized ID for both <label> and <input type="password" /> as the label surrounds the input field.

So it would look like this:

$output = '<form action="' . get_option('siteurl') . '/wp-pass.php" class="post-password-form" method="post"> 
<p>' . __("This post is password protected. To view it please enter your password below:") . '</p> 
<p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label>
<input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>';

All of the above HTML can be styled with .post-password-form p, .post-password-form label input and even .post-password-form input[type="submit"] if needed.

Taking into account Andrew Ozz's suggestions

comment:8 in reply to: ↑ 7   philiparthurmoore20 months ago

Replying to azaozz:

Looking at get_the_password_form.patch we won't need 3 classes to style 3 elements. A post-password-form class on the <form> is enough. Also we don't need a randomized ID for both <label> and <input type="password" /> as the label surrounds the input field.

So it would look like this:

$output = '<form action="' . get_option('siteurl') . '/wp-pass.php" class="post-password-form" method="post"> 
<p>' . __("This post is password protected. To view it please enter your password below:") . '</p> 
<p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label>
<input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>';

All of the above HTML can be styled with .post-password-form p, .post-password-form label input and even .post-password-form input[type="submit"] if needed.

Thank you very much for your suggestions and input, @azaozz. Cheers.

Note: See TracTickets for help on using tickets.