下面是一个有趣的演示测试,看看这是否可行:
演示首先,我们按照通常的方式设置帖子的密码:
然后我们创建一个名为
wpse_extra_passwords
采用逗号分隔的密码:
这些是该帖子的额外密码。
让我们根据post_password_required()
核心功能:
/**
* Helper function, check password + extra passwords
*/
function wpse_post_password_required( $post = null )
{
$post = get_post($post);
if ( empty( $post->post_password ) )
return false;
if ( ! isset( $_COOKIE[\'wp-postpass_\' . COOKIEHASH] ) )
return true;
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\' ) )
return true;
// Check the current password
if( $hasher->CheckPassword( $post->post_password, $hash ) )
return false;
// Fetch extra passwords
if( ! $extra_passwords = get_post_meta( $post->ID, \'wpse_extra_passwords\', true ) )
return true;
// Check these extra passwords
$extra = explode( \',\', $extra_passwords );
foreach( (array) $extra as $password )
{
$password = trim( $password );
if( ! empty( $password ) && $hasher->CheckPassword( $password, $hash ) )
return false;
}
return true;
}
然后,我们将
the_password_form
在主循环中筛选并定位单个post对象:
/**
* Support extra post passwords for single posts in the main loop
*/
add_filter( \'the_password_form\', function( $output )
{
if( ! is_single() || ! in_the_loop() || did_action( \'the_password_form\' ) )
return $output;
$post = get_post();
// Display password form if none of the passwords matches:
if( wpse_post_password_required( $post ) )
return $output;
// Get the current password
$password = $post->post_password;
// Temporary remove it
$post->post_password = \'\';
// Fetch the content
$content = get_the_content();
// Set the password back
$post->post_password = $password;
return $content;
} );
希望你可以测试它,并进一步使用它。
注意,您提到了2314之类的密码。编写一个能猜出这样简单密码的程序很容易。所以我在这个演示中使用了更强大的密码。