WordPress 短代码:关注微信公众号获取查看验证码

添加短代码

以WordPress默认主题二〇一九为例,将下面的代码,添加到当前主题函数模板 functions.php 的最后:

function weixin_fans($atts, $content=null){
	extract(shortcode_atts( array (
		'key' => null,
		'reply' => ''
		), $atts));
	// 管理员登录直接显示隐藏内容
	if ( current_user_can('level_10') ) {
		return '<div class="hidden-content">'.do_shortcode( $content ).'</div>';
	}
	// 输入正确验证码显示隐藏内容
	if(isset($_POST['weixin_key']) && $_POST['weixin_key']==$key){
		return '<div class="hidden-content">'.do_shortcode( $content ).'</div>';
	} else {
		return '
		<form class="post-password-form weixin-form" action="'.get_permalink().'" method="post">
			<div class="weixin-box">
				<div class="post-secret"><i class="be be-info"></i>输入验证码查看隐藏内容:</div>
				<p>
					<input id="wpbox" type="password" size="20" name="weixin_key">
					<input type="submit" value="提交" name="Submit">
				</p>
				<div class="weixin-secret">
					<div class="weixin-w">扫描二维码关注本站微信公众号或者在微信里搜索:<span class="weixin-n">你的微信公众号名称</span></div>
					<div class="weixin-w">回复 <span class="weixin-n">'.$reply.'</span> 获取验证码</div>
					<img src="https://wx3.sinaimg.cn/large/0066LGKLly1g6fs5fgkg6j30nw0nwq5e.jpg" alt="weixin">
				</div>
			</div>
		</form>';
	}
}
add_shortcode('weixin', 'weixin_fans');

修改其中的:你的微信公众号名称

提示:因代码中有中文,所以修改保存 functions.php 文件时,模版编码选择:UTF-8 无BOM(无签名),否则中文会显示乱码。

配套样式:

.hidden-content, .weixin-form {
	padding: 0 0.5rem;
	border: 1px dashed #ff4400;
}
.weixin-n {
	color: #ff4400;
}
.weixin-form img {
	width: 150px;
	height: auto;
}

添加到主题样式文件中或者外观 → 自定义 → 额外CSS中

使用方法:

编辑文章时输入:

[weixin key=验证码 reply=回复的内容]隐藏的内容[/weixin]

分别输入自动回复的验证码和回复的内容

提示:

要注意验证码后面有一个半角的空格,否则输入验证码无效。

代码中无cookie功能,再次打开页面需要重新输入验证码。

管理员登录状态下直接显示隐藏的内容,不需要输入验证码。

如何添加微信公众号自动回复,请自行百度。

短代码虽然目前已被区块编辑器所取代,不过短代码功能还是可以正常使用的。

@版权声明: 本站所有文章,如左上角无特殊说明,则归类于图图网原创,其它版权归原作者所有,如若本站内容侵犯了您的合法权益,可联系我们进行处理。

给TA支持
共{{data.count}}人
人已支持
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧