post_password_required

Whether post requires password and correct password has been provided.

Description

post_password_required( (null) $post = null ); 

Parameters (1)

0. $post — Optional. (null) => null
An optional post. Global $post used if not provided.

Usage

  1. if ( !function_exists( 'post_password_required' ) ) { 
  2. require_once ABSPATH . WPINC . '/post-template.php'; 
  3.  
  4. // An optional post. Global $post used if not provided. 
  5. $post = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = post_password_required($post); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post-template.php  
  1. function post_password_required( $post = null ) { 
  2. $post = get_post($post); 
  3.  
  4. if ( empty( $post->post_password ) ) { 
  5. /** This filter is documented in wp-includes/post.php */ 
  6. return apply_filters( 'post_password_required', false, $post ); 
  7.  
  8. if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) { 
  9. /** This filter is documented in wp-includes/post.php */ 
  10. return apply_filters( 'post_password_required', true, $post ); 
  11.  
  12. $hasher = new PasswordHash( 8, true ); 
  13.  
  14. $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ); 
  15. if ( 0 !== strpos( $hash, '$P$B' ) ) { 
  16. $required = true; 
  17. } else { 
  18. $required = ! $hasher->CheckPassword( $post->post_password, $hash ); 
  19.  
  20. /** 
  21. * Filters whether a post requires the user to supply a password. 
  22. * @since 4.7.0 
  23. * @param bool $required Whether the user needs to supply a password. True if password has not been 
  24. * provided or is incorrect, false if password has been supplied or is not required. 
  25. * @param WP_Post $post Post data. 
  26. */ 
  27. return apply_filters( 'post_password_required', $required, $post );