函数文档

post_password_required()

💡 云策文档标注

概述

post_password_required() 函数用于判断文章是否需要密码保护以及用户是否提供了正确的密码。它检查文章密码设置和 cookie 验证,返回布尔值表示密码需求状态。

关键要点

  • 函数接受一个可选参数 $post,可以是整数、WP_Post 对象或 null,默认使用全局 $post。
  • 返回 false 表示不需要密码或密码正确,true 表示需要密码或密码错误。
  • 内部逻辑:先检查文章是否有密码,再验证 cookie 中的密码哈希是否匹配。
  • 应用了 'post_password_required' 过滤器,允许开发者自定义判断逻辑。
  • 相关函数包括 get_post()、wp_unslash() 和 apply_filters()。

代码示例

if ( post_password_required( $post->ID ) )
{
    echo get_the_password_form();
    echo 'THIS POST IS PASSWORD PROTECTED: PLEASE ENTER IT!';
}
else
{
    if ( have_posts() )
    { 
        while ( have_posts() ) 
        {        
            the_post();
            the_content();
            echo '';
         }    
    }
    else
    {
        echo 'Nothing Found!';
    }
}

注意事项

  • 使用时需获取文章 ID 作为参数,否则函数可能无法正常工作。
  • 密码 cookie 设置后会影响调试,建议在无痕模式下测试代码。
  • 函数自 WordPress 2.7.0 版本引入,广泛用于内容显示、REST API 和评论处理等场景。

📄 原文内容

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

Parameters

$postint|WP_Post|nulloptional
An optional post. Global $post used if not provided.

Default:null

Return

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

Source

function post_password_required( $post = null ) {
	$post = get_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( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
		/** 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( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
	if ( ! str_starts_with( $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 WP_Post $post     Post object.
	 */
	return apply_filters( 'post_password_required', $required, $post );
}

Hooks

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

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

Changelog

Version Description
2.7.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

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

    Note 1: Without getting, (and using the post->ID), then the post_password_required function doesn’t work.
    Note 2: Something else you have to watch is cookies being set once the password has been used once, makes debugging a nightmare, (tip), use an Incognito Window when testing out your code.
    Note 3: I have broken 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.

    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>';
        }
    }
    ?>