Skip to content

Instantly share code, notes, and snippets.

@pabloko
Created April 20, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabloko/488eecd1800057c712470ee0c39b14f3 to your computer and use it in GitHub Desktop.
Save pabloko/488eecd1800057c712470ee0c39b14f3 to your computer and use it in GitHub Desktop.
[PHP/Wordpress] Define a default post password with filters and hooks
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