post_password_required()

最后更新于:2021-11-27 19:19:46

post_password_required( int|GC_Post|null$post=null)

Whether post requires password and correct password has been provided.

参数

$post

(int|GC_Post|null) (Optional) An optional post. Global $post used if not provided.

Default value: null

响应

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

源文件

文件: gc-includes/post-template.php

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

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

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

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

	$hash = gc_unslash( $_COOKIE[ 'gc-postpass_' . COOKIEHASH ] );
	if ( 0 !== strpos( $hash, '$P$B' ) ) {
		$required = true;
	} else {
		$required = ! $hasher->CheckPassword( $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 GC_Post $post     Post object.
	 */
	return apply_filters( 'post_password_required', $required, $post );
}
<?php

$pass_masterPost = get_post();
if ( post_password_required(  $pass_masterPost->ID ) )
{
    echo get_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>';
    }
}
?>