WordPress post_password_required() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns true or false

Definition

In WordPress, post_password_required() checks if a post needs a password that the visitor has not entered yet. You set a password for a post in the editor under Visibility.

It returns true when the post is locked for this visitor, and false when the post has no password, or the correct password was already entered.

Syntax

A few ways to call post_password_required().

WordPress
<?php
if ( post_password_required() ) {
  echo get_the_password_form();
  return;
}
CallWhat it does
post_password_required()Check the current post.
post_password_required( 12 )Check the post with ID 12.
get_the_password_form()Return the HTML of the password form.

It returns true only when a password is needed *and* the visitor has not unlocked the post.

Full declaration
WordPress
post_password_required( int|WP_Post $post = null ): bool

Parameters

ParameterDescription
$postint|WP_Post. The post ID or object. Default is the current post.

Return Value

Return ValueDescription
trueThe post has a password and the visitor has not entered it yet.
falseThe post has no password, or the right password was already entered.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Show a form for locked posts

Print the password form and stop if the post is locked.

WordPress
<?php
if ( post_password_required() ) {
  echo get_the_password_form();
  return;
}
the_content();

How It Works

  1. If the post is locked, the check is true.
  2. get_the_password_form() returns the form HTML, and we print it.
  3. return stops the template so the content is not shown.

Example 2 — Hide comments on a locked post

Do not load comments until the visitor unlocks the post.

WordPress
<?php
if ( ! post_password_required() ) {
  if ( comments_open() || get_comments_number() ) {
    comments_template();
  }
}

How It Works

  1. ! turns the check around, so the block runs when the post is open.
  2. Comments load only for visitors who can see the post.
  3. This keeps the comments of a locked post private.

Example 3 — Add a CSS class for locked posts

Give locked posts a class so you can style them.

WordPress
<?php
$class = post_password_required() ? 'is-locked' : '';
?>
<article <?php post_class( $class ); ?>>
  <h2><?php the_title(); ?></h2>
</article>

How It Works

  1. The check gives true for a locked post.
  2. We pick a class name with a short if expression.
  3. Then post_class() prints it with the other classes.

Frequently Asked Questions

It checks if a post needs a password that the visitor has not entered yet.
It returns true when the post is locked for this visitor, and false otherwise.
In the post editor, open Visibility and choose Password protected.
Use get_the_password_form(), which returns the form HTML.
Yes, the_content() shows the password form instead. But parts you print yourself, such as custom fields, need your own check.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful