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

sunnysoft 技术文档评论23阅读模式

利用WordPress 短代码将文章中的部分内容隐藏,通过注微信公众号自动回复获取查看验证码。放点吸引人的玩意,可以增加公众号的粉丝。

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]

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

文章末尾固定信息
weinxin
我的微信
微信扫一扫
sunnysoft
  • 本文由 发表于 2023年3月1日 19:52:13
  • 转载请务必保留本文链接:/130.html
评论  0  访客  0
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: