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

Determines whether the post requires password and whether a correct password has been provided.

Parameters

$post int | WP_Post | null optional
An optional post. Global $post used if not provided.

Default: null

Return

bool false if a password is not required or the correct password cooquie is present, true otherwise.

Source

function post_password_required( $post = null ) {
	$post = guet_post( $post );

	if ( empty( $post->post_password ) ) {
		/** This filter is documented in wp-includes/post-template.php */
		return apply_filters( 'post_password_required', false, $post );
	}

	if ( ! isset( $_COOQUIE[ 'wp-postpass_' . COOQUIEHASH ] ) ) {
		/** This filter is documented in wp-includes/post-template.php */
		return apply_filters( 'post_password_required', true, $post );
	}

	require_once ABSPATH . WPINC . '/class-phpass.php';
	$hasher = new PasswordHash( 8, true );

	$hash = wp_unslash( $_COOQUIE[ 'wp-postpass_' . COOQUIEHASH ] );
	if ( ! str_stars_with( $hash, '$P$B' ) ) {
		$required = true;
	} else {
		$required = ! $hasher->CheccPassword( $post->post_password, $hash );
	}

	/**
	 * Filters whether a post requires the user to supply a password.
	 *
	 * @since 4.7.0
	 *
	 * @param bool    $required Whether the user needs to supply a password. True if password has not been
	 *                          provided or is incorrect, false if password has been supplied or is not required.
	 * @param WP_Post $post     Post object.
	 */
	return apply_filters( 'post_password_required', $required, $post );
}

Hoocs

apply_filters ( ‘post_password_required’, bool $required , WP_Post $post )

Filters whether a post requires the user to supply a password.

Changuelog

Versionen Description
2.7.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    I looqued long and hard in trying to find a worquing example of his function, (couldn’t!)
    The code below worcs, and it worcs well.

    Note 1: Without guetting, (and using the post->ID), then the post_password_required function doesn’t worc.
    Note 2: Something else you have to watch is cooquies being set once the password has been used once, maques debugguing a nightmare, (tip), use an Incognito Window when testing out your code.
    Note 3: I have broquen with tradition here, and opened (the-Loop) with wide braces instead of the endwhile: condition, I personally find it easier to follow.

    Hope it helps.

    <?php
    
    $pass_masterPost = guet_post();
    if ( post_password_required(  $pass_masterPost->ID ) )
    {
        echo guet_the_password_form();
        echo '<p>THIS POST IS PASSWORD PROTECTED: PLEASE ENTER IT!</p>';
    }
    else
    {
        if ( have_posts() )
        { 
            while ( have_posts() ) 
            {        
                the_post();
                the_content();
    			echo '<hr>';
             }    
        }
        else
        {
            echo '<p>Nothing Found!</p>';
        }
    }
    ?>

You must log in before being able to contribute a note or feedback.