以前就经常看见很多网站用这个功能,自己也用不到,觉得对用户体验不好。

但是最近重启了公众号宣传,就把其中一个网站也做了这个功能,网上搜索了下,其实就是把Wordpress自带的输入密码查看改成了走公众号的形式。

代码已经整理好,可能只是css因为不同的主题会错位,自己调整下就行。

一:添加以下代码到functions.php文件

//公众号
function weixin_guanzhu($atts, $content=null){
	extract(shortcode_atts( array (
		'key' => null,
		'replykey' => ''
		), $atts));
	// 管理员默认显示隐藏内容
	if ( current_user_can('level_10') ) {
		return '<div class="weixinguanzhu">'.do_shortcode( $content ).'</div>';
	}
	// 输入正确验证码显示隐藏内容
	if(isset($_POST['weixin_key']) && $_POST['weixin_key']==$key){
		return '<div class="secret-password">'.do_shortcode( $content ).'</div>';
	} else {
		return '
		<form action="'.get_permalink().'" method="post">
			<div class="weixinguanzhu">
			<img class="erweima" align="right" src="二维码地址" width="150" height="150" alt="你的二维码名字"><div class="post-secret"><i class="fa fa-exclamation-circle"></i>此内容不用注册登录,关注公众号获取验证码查看就行</div><div class="details">请关注“xxxx”官方微信公众号<br>用手机扫描右侧二维码快速关注<br>回复关键字“<span>'.$replykey.'</span>”,获取验证码</div>
				<p>
					<input id="wpbox" type="password" size="20" name="weixin_key">
					<input type="submit" value="提交验证码查看" name="Submit">
				</p>
				</div>
		</form>';
	}
}
add_shortcode('weixin', 'weixin_guanzhu');

二:根据自己的主题添加css样式

.weixinguanzhu,.secret-password {
	background:none repeat scroll 0 0 #0db795;
	border-left:5px solid #ffe700;
	color:#443a3a;
	padding:10px 0 10px 10px;
	border-radius:5px;
	margin-bottom:15px;
	overflow:hidden;
	clear:both;
}
.weixinguanzhu .post-secret {
	font-size:18px;
	line-height:20px;
	color:#f7eb00;
	margin:5px;
}
.weixinguanzhu form {
	margin:15px 0;
}
.weixinguanzhu form span {
	font-size:18px;
	font-weight:700;
}
.weixinguanzhu .erweima {
	margin-left:20px;
	margin-right:16px;
}
.weixinguanzhu input[type=password] {
	color:#9ba1a8;
	padding:6px;
	background-color:#F7F7F7;
	border:1px solid #E8E6E5;
	font-size:12px;
	width:50%;
	-moz-transition:border .25s linear,color .25s linear,background-color .25s linear;
	-webkit-transition:border .25s linear,color .25s linear,background-color .25s linear;
	-o-transition:border .25s linear,color .25s linear,background-color .25s linear;
	transition:border .25s linear,color .25s linear,background-color .25s linear;
}
.weixinguanzhu input[type=submit] {
	background:#F43405;
	border:none;
	width:200px;
	height:40px;
	color:#fff;
	outline:0;
	font-size:16px;
	border-radius: 2em;
    box-shadow: 2px 2px 5px #0DF2C4;
}
.weixinguanzhu .details span {
	color:#f7eb00;
}

三:编辑文章的时候加入代码就行

[weixin key=验证码 replykey=发送到公众号的内容]隐藏的内容[/weixin]

四:设置公众号自动回复

用户发送给公众号的内容:对应replykey

公众号自动回复的内容:对应key

实际上key相当于就是Wordpress自带的文章密码。