post_password_required()
Whether post requires password and correct password has been provided.
Hooks from the function
Return
true|false
. false if a password is not required or the correct password cookie is present, true otherwise.
Usage
post_password_required( $post );
- $post(int|WP_Post|null)
- An optional post. Global $post used if not provided.
Default: null
Examples
#1 Only valid for password-protected posts
Let's assume that post 443 is password protected, then:
if( post_password_required( 443 ) ){ echo "This post is password protected!"; }
#2 Show posts only if user has post password
I looked long and hard in trying to find a working example of his function, (couldn’t!)
$master_post = get_post(); if ( post_password_required( $master_post->ID ) ) { echo '<p>THIS POST IS PASSWORD PROTECTED: PLEASE ENTER IT!</p>'; echo get_the_password_form(); } else { if ( have_posts() ) { while ( have_posts() ) { the_post(); the_content(); echo '<hr>'; } } else { echo '<p>Nothing Found!</p>'; } }
Note: 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.
See: get_the_password_form(),
Changelog
Since 2.7.0 | Introduced. |