纯代码实现文章部分内容关注微信公众号后可见——WordPress美化

2017年11月11日01:13:30 2 5,512

仙踪小栈之前分享过有这么一个WordPress插件,关注公众号并回复关键字获取验证码后,才能查看到文章中的隐藏内容。一直没有找到这个代码版的,前几天想起了这个功能,于是今天便找到了这个分享给大家。

 

纯代码实现文章部分内容关注微信公众号后可见——WordPress美化

 

一、核心代码

将以下代码放入functions.php中(具体效果请前往:传送门):

  1. /**
  2.  * WordPress文章部分内容关注微信公众号后可见 - 龙笑天下
  3.  * http://www.ilxtx.com/view-contents-by-following-wechat-official-accounts.html
  4.  */
  5. function lxtx_secret_content($atts$content=null){
  6.  extract(shortcode_atts(array('key'=>null,'keyword'=>null), $atts));
  7.  if(isset($_POST['secret_key']) && $_POST['secret_key']==$key){
  8.  return '<div class="secret-password">'.$content.'</div>';
  9.  } else {
  10.  return
  11.  '<div class="post_hide_box">
  12.  <img class="erweima" align="right" src="改为二维码图片的网址" width="150" height="150" alt="你的二维码名字"><div class="post-secret"><i class="fa fa-exclamation-circle"></i>此处内容已经被作者无情的隐藏,请输入验证码查看内容</div>
  13.  <form action="'.get_permalink().'" method="post">
  14.  <span>验证码:</span><input id="pwbox" type="password" size="20" name="secret_key">
  15.  <a class="a2" href="javascript:;"><input type="submit" value="提交" name="Submit"></a>
  16.  </form>
  17.  <div class="details">请关注“你的名字”官方QQ公众号,回复关键字“<span>'.$keyword.'</span>”,获取验证码。【注】用手机QQ扫描右侧二维码都可以关注“你的名字”官方QQ公众号。</div>
  18.  </div>';
  19.  }
  20. }
  21. add_shortcode('gzh2v', 'lxtx_secret_content');

PS:注意修改代码中的相关内容!

 

二、前端CSS样式

将以下代码放到你主题的style.css文件中:

  1. .post_hide_box, .secret-password{backgroundnone repeat scroll 0 0 #efe;border-left5px solid #e74c3c;color#555;padding10px 0 10px 10px;border-radius: 5px;margin-bottom15px;overflow:hiddenclear:both;}
  2. .post_hide_box .post-secret{font-size18pxline-height:20pxcolor:#e74c3cmargin:5px;}
  3. .post_hide_box form{ margin:15px 0;}
  4. .post_hide_box form span{ font-size:18pxfont-weight:700;}
  5. .post_hide_box .erweima{ margin-left:20pxmargin-right:16px;}
  6. .post_hide_box input[type=password]{ color#9ba1a8padding6pxbackground-color#f6f6f6border1px solid #e4e6e8font-size12px;-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;}
  7. .post_hide_box input[type=submit] { background#F88C00bordernoneborder2px solid;border-color#F88C00border-leftnoneborder-topnonepadding0px;width100pxheight38pxcolor#fffoutline: 0;border-radius: 0 0 2px 0; font-size16px;}
  8. .post_hide_box .details span{color:#e74c3c;}

 

三、集成短代码

为了方便使用,在后台文本编辑器中集成该短代码。将以下代码放入 functions.php 中即可:

  1. // 后台文本编辑框中添加公众号隐藏简码按钮
  2. function lxtx_wpsites_add_gzh_quicktags() {
  3.     if (wp_script_is('quicktags')){
  4. ?>
  5.     <script type="text/javascript">
  6.     QTags.addButton( 'gzh2v', ' 公众号隐藏', '\n[gzh2v keyword="关键字" key="验证码"]隐藏内容[/gzh2v]', "" );
  7.     </script>
  8. <?php
  9.     }
  10. }
  11. add_action( 'admin_print_footer_scripts', 'lxtx_wpsites_add_gzh_quicktags' );

 

四、使用方法

4.1 在文章中使用短代码

插入以下简码,修改相关内容即可:

  1. [gzh2v keyword="关键字" key="验证码"]隐藏内容[/gzh2v]

4.2 微信公众号

这里以微信公众号为例(QQ公众号应该也是这样),在微信公众号的“自动回复-关键字自动回复”中设置好关键字(对应上面代码中的keyword)及验证码(对应上面代码中的key)。

 

五、效果图

纯代码实现文章部分内容关注微信公众号后可见——WordPress美化

 

六、延伸

其实本文是跟常见的“文章部分内容回复后看见”或“文章部分内容登录后可见”的代码原理一致,只是结合了一下公众号而已。大家可以动动手集成到自己的主题哈!

这个功能先收集到这里,各位小伙伴是否需要使用。这个功能对于公众号吸粉还是不错的。

 

七、相关链接

必须关注微信公众号才能查看隐藏内容——WordPress插件

 

 

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如果喜欢,请打赏支持本站,谢谢大家!

发表评论

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

目前评论:2   其中:访客  0   博主  0   引用   1

    • 天道酬勤 天道酬勤 0 来自天朝的朋友 广东省珠海市 电信

      有没有办法实现微信强制分享呢?

    • 来自外部的引用: 1

      • 必须关注微信公众号才能查看隐藏内容——WordPress插件 – 仙踪小栈