本文最后更新于 2024-12-13,已超过 365 天未更新,内容可能已失效。
WP教程:
这个不用新建文件,直接把下面代码加到主题functions.php文件即可使用。区别于wordpress的密码保护,这个可以设置多个文章和密码。
[rihide]
function check_post_access() {
if (is_single()) {
global $post;
// 手动设置要保护的文章ID和对应的密码
$protected_posts = [
'123,456,789' => 'sharedpassword1', // 多个文章ID => 密码
'101,102,103' => 'sharedpassword2',
// 添加更多的文章ID和密码
];
foreach ($protected_posts as $post_ids => $access_password) {
$post_id_array = explode(',', $post_ids);
if (in_array($post->ID, $post_id_array)) {
if (isset($_COOKIE['post_access_verified_' . $post->ID])) {
return; // 已验证,返回
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['pwd']) && $_POST['pwd'] === $access_password) {
setcookie('post_access_verified_' . $post->ID, 'true', time() + 3600, COOKIEPATH, COOKIE_DOMAIN, false, true);
wp_redirect(get_permalink());
exit;
} else {
add_action('template_redirect', function() {
show_password_protected_page('密码错误,请重试');
});
return;
}
} else {
add_action('template_redirect', function() {
show_password_protected_page();
});
return;
}
}
}
}
}
add_action('wp', 'check_post_access');
function show_password_protected_page($title = '') {
ob_start(); ?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo esc_html($title); ?></title>
<style type="text/css">
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 0 15px;
}
.card {
width: 100%;
max-width: 500px;
padding: 30px;
border-radius: 0.375rem;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
background-color: #ffffff;
text-align: center;
}
.svg-container {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 10px;
}
.svg-container svg {
width: 160px;
height: 160px;
}
</style>
<?php wp_head(); ?>
</head>
<body>
<div class="container">
<div class="col-12 card">
<div class="svg-container">
<svg class="alert" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#FF9800">
<path d="M12 2C9.24 2 7 4.24 7 7v3H6c-1.1 0-2 .9-2 2v8c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2v-8c0-1.1-.9-2-2-2h-1V7c0-2.76-2.24-5-5-5zm-4 8V7c0-2.21 1.79-4 4-4s4 1.79 4 4v3H8zm4 4c.83 0 1.5.67 1.5 1.5S12.83 17 12 17s-1.5-.67-1.5-1.5S11.17 14 12 14zm-6 4v-6h12v6H6z"/>
</svg>
</div>
<h2 class="mb-3"><?php echo esc_html($title);?></h2>
<form action="" method="post" class="form">
<div class="mb-3">
<div class="form-outline" data-mdb-input-init>
<input type="password" name="pwd" class="form-control" required>
<label class="form-label"><?php _e('请输入访问密码查看', 'zptheme');?></label>
</div>
</div>
<div class="dhstack gap-3">
<button type="submit" class="btn btn-outline-primary px-5"><?php _e('提交', 'zptheme');?></button>
<div class="vr"></div>
<a href="<?php echo esc_url(home_url('/')); ?>" class=" btn btn-outline-success px-5" title="点击回到网站首页"><?php _e('- 返回首页 -', 'zptheme');?></a>
</div>
</form>
</div>
</div>
<?php wp_footer(); ?>
</body>
</html>
<?php
echo ob_get_clean();
exit;
}
[/rihide]
当然最好要排除搜索蜘蛛爬虫
最后附上演示
点击查看演示除非注明,子佩工作室 文章均为原创,转载请以链接形式标明本文地址。




登录后即可发表评论
全部评论 (2)
做决定之前仔细考虑,一旦作了决定就要勇往直前、坚持到底。
人生在世,不出一番好议论,不留一番好事业,终日饱食暖衣,不所用心,何自别于禽兽。