Created
April 20, 2020 13:32
-
-
Save pabloko/488eecd1800057c712470ee0c39b14f3 to your computer and use it in GitHub Desktop.
[PHP/Wordpress] Define a default post password with filters and hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'post_password_required', function( $returned, $post ) | |
{ | |
//Define here your conditional logic, example filter posts without password from custom post type "photo_gallery" | |
if (empty($post->post_password) && $post->post_type=="photo_galery") | |
$post->post_password='Secretpw123'; | |
//End of conditional logic. Returning true will ask for password, false will display the content. | |
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 ); | |
return $required; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment