wppaste
WordPress

get_the_password_form( int|WP_Post $post = 0 ): string

Since
1.0.0
Source
wp-includes/post-template.php:1776
Retrieves protected post password form content.

Compatibility

WordPress
since 1.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.Default: 0

Return value

string
HTML content for password form for password-protected post.

Performance profile

How much work a call to get_the_password_form() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
61–108

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 109.

Plugin surface
2 hooks

Third-party callbacks on 'the_password_form_incorrect_password', 'the_password_form' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • transienttransientget_transient()one call below get_the_password_form()
  • optionoption read or writeget_option()one call below get_the_password_form()

Further down the call graph this can also reach cache and serialize. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_the_password_form() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always61get_post(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
empty($post)64get_post(), wp_rand(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
always70–75get_post(), wp_get_raw_referer(), get_permalink(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
always73get_post(), get_permalink(), esc_attr(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
always73–78get_post(), wp_rand(), wp_get_raw_referer(), get_permalink(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
always76get_post(), wp_rand(), get_permalink(), esc_attr(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
!empty($post)82–87get_post(), wp_get_raw_referer(), get_permalink(), get_permalink(), esc_attr(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
always85–90get_post(), wp_rand(), wp_get_raw_referer(), get_permalink(), get_permalink(), esc_attr(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
isset($value)93get_post(), wp_get_raw_referer(), get_permalink(), __(), apply_filters(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
isset($value)96get_post(), wp_rand(), wp_get_raw_referer(), get_permalink(), __(), apply_filters(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
!empty($post) && isset($value)105get_post(), wp_get_raw_referer(), get_permalink(), __(), apply_filters(), get_permalink(), esc_attr(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()
isset($value)108get_post(), wp_rand(), wp_get_raw_referer(), get_permalink(), __(), apply_filters(), get_permalink(), esc_attr(), site_url(), esc_url(), __(), __(), esc_attr_x(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10861–10751 fewer instruction than PHP 8.5
8.510961–1085
8.410961–10851 fewer instruction than PHP 8.3
8.311061–1095
8.211061–1095
8.111061–10951 fewer instruction than PHP 7.4
7.411162–1095

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while get_the_password_form() runs, in this order:

  1. apply_filters( the_password_form_incorrect_password )filterline 1796 (+20 into the body)

    Filters the invalid password message shown on password-protected posts.

  2. apply_filters( the_password_form )filterline 1830 (+54 into the body)

    Filters the HTML output for the protected post password form.

Uses · 10

  • get_post()Retrieves post data given a post ID or post object.
  • wp_rand()Generates a random non-negative number.
  • wp_get_raw_referer()Retrieves unvalidated referer from the '_wp_http_referer' URL query variable or the HTTP referer.
  • get_permalink()Retrieves the full permalink for the current post or post ID.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • __()Retrieves the translation of $text.
  • esc_attr()Escaping for HTML attributes.
  • esc_url()Checks and cleans a URL.
  • site_url()Retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
  • esc_attr_x()Translates string with gettext context, and escapes it for safe use in an attribute.

Used by · 1

Source code

function get_the_password_form( $post = 0 ) {	$post                  = get_post( $post );	$field_id              = 'pwbox-' . ( empty( $post->ID ) ? wp_rand() : $post->ID );	$invalid_password      = '';	$invalid_password_html = '';	$aria                  = '';	$class                 = '';	$redirect_field        = ''; 	// If the referrer is the same as the current request, the user has entered an invalid password.	if ( ! empty( $post->ID ) && wp_get_raw_referer() === get_permalink( $post->ID ) && isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {		/**		 * Filters the invalid password message shown on password-protected posts.		 * The filter is only applied if the post is password-protected.		 *		 * @since 6.8.0		 *		 * @param string  $text The message shown to users when entering an invalid password.		 * @param WP_Post $post Post object.		 */		$invalid_password      = apply_filters( 'the_password_form_incorrect_password', __( 'Invalid password.' ), $post );		$invalid_password_html = '<div class="post-password-form-invalid-password" role="alert"><p id="error-' . $field_id . '">' . $invalid_password . '</p></div>';		$aria                  = ' aria-describedby="error-' . $field_id . '"';		$class                 = ' password-form-error';	} 	if ( ! empty( $post->ID ) ) {		$redirect_field = sprintf(			'<input type="hidden" name="redirect_to" value="%s" />',			esc_attr( get_permalink( $post->ID ) )		);	} 	$output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form' . $class . '" method="post">' . $redirect_field . $invalid_password_html . '	<p>' . __( 'This content is password-protected. To view it, please enter the password below.' ) . '</p>	<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>	'; 	/**	 * Filters the HTML output for the protected post password form.	 *	 * If modifying the password field, please note that the WordPress database schema	 * limits the password field to 255 characters regardless of the value of the	 * `minlength` or `maxlength` attributes or other validation that may be added to	 * the input.	 *	 * @since 2.7.0	 * @since 5.8.0 Added the `$post` parameter.	 * @since 6.8.0 Added the `$invalid_password` parameter.	 *	 * @param string  $output           The password form HTML output.	 * @param WP_Post $post             Post object.	 * @param string  $invalid_password The invalid password message.	 */	return apply_filters( 'the_password_form', $output, $post, $invalid_password );}

Changelog

Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/post-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.