post_password_required

函数
post_password_required ( $post = null )
参数
  • (int|WP_Post|null) $post An optional post. Global $post used if not provided.
    Required:
    Default: null
返回值
  • (bool) false if a password is not required or the correct password cookie is present, true otherwise.
定义位置
相关方法
wp_lostpassword_urlthe_post_passwordwp_set_passwordget_password_reset_keywp_get_password_hint
引入
2.7.0
弃用
-

post_password_required是一个WordPress函数,用于检查一个文章是否有密码保护,以及当前用户是否输入了正确的密码。如果文章是受密码保护的,而用户没有输入正确的密码,这个函数将返回true,否则将返回false。

确定文章是否需要密码,以及是否已经提供了正确的密码。

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 ( 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 WP_Post $post     Post object.
	 */
	return apply_filters( 'post_password_required', $required, $post );
}

常见问题

FAQs
查看更多 >