WordPress主题纯代码实现文章设置隐藏内容公众号可见教程

1 核心代码

     部分转自钻芒博客,其它代码又幂彀社区和菜鸟站长之家代改进而来,不会影响图片灯箱。将以下代码放入 functions.php 中:

/**
* WordPress文章部分内容关注微信公众号后可见
*/
function weixingzh_secret_content($atts, $content=null){
    extract(shortcode_atts(array('key'=>null,'keyword'=>null), $atts));
    if(isset($_POST['secret_key']) && $_POST['secret_key']==$key){
        return '<div class="secret-password">'.$content.'</div>';
    } else {
        return '<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
        <div class="gzhhide">
            <div class="gzhtitle">抱歉!隐藏内容,请输入密码后可见!<i class="fa fa-lock"></i><span></span></div>
                <div class="gzh-content">请打开微信扫描右边的二维码回复关键字“<span><b>'.$keyword.'</b></span>”获取密码,也可以微信直接搜索“科技小新”关注微信公众号获取密码。
                <div class="gzhcode" style="background: url(https://www.cnzzzj.com/wp-content/uploads/2020/09/wxgzh.jpg);background-size: 100%;" width="140" height="140" alt="菜鸟福利之家"></div>
            </div>
        <div class="gzhbox"><form action="'.get_permalink().'" method="post">
        <input id="pwbox" type="password" size="20" name="secret_key">
        <button type="submit">立即提取</button></form></div></div>';
    }
}
add_shortcode('weixingzh', 'weixingzh_secret_content');
// 后台文本编辑框中添加公众号隐藏简码按钮
function wpsites_add_weixingzh_quicktags() {
    if (wp_script_is('quicktags')){
?>
    <script type="text/javascript">
    QTags.addButton( 'weixingzh', '公众号隐藏', '[weixingzh keyword="关键字" key="验证码"]隐藏内容[/weixingzh]',"" );
    </script>
<?php
    }
}
add_action( 'admin_print_footer_scripts', 'wpsites_add_weixingzh_quicktags' );

2 前端 CSS 样式一

之前有很多小伙伴反应css链入错误。大家别用链入方法了,直接把下边的css丢到主题的style.css即可。本次还修复了 小锁图标倾斜的问题,然后把上面的https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css外链CSS去掉。

/**  纯代码实现WordPress文章设置隐藏内容公众号可见*/ 
.post_hide_box, .secret-password{background: none repeat scroll 0 0 #efe;border-left: 5px solid #e74c3c;color: #555;padding: 10px 0 10px 10px;border-radius: 5px;margin-bottom: 15px;overflow:hidden; clear:both;}
.post_hide_box .post-secret{font-size: 18px; line-height:20px; color:#e74c3c; margin:5px;}
.post_hide_box form{ margin:15px 0;}
.post_hide_box form span{ font-size:18px; font-weight:700;}
.post_hide_box .erweima{ margin-left:20px; margin-right:16px;}
.post_hide_box input[type=password]{ color: #9ba1a8; padding: 6px; background-color: #f6f6f6; border: 1px solid #e4e6e8; font-size: 12px;-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;}
.post_hide_box input[type=submit] { background: #F88C00; border: none; border: 2px solid;border-color: #F88C00; border-left: none; border-top: none; padding: 0px;width: 100px; height: 38px; color: #fff; outline: 0;border-radius: 0 0 2px 0; font-size: 16px;}
.post_hide_box .details span{color:#e74c3c;}
.post_hide_box .details 
span{color:#e74c3c;}
.gzhhide{background:#fff;border-radius:10px;padding:20px;margin:15px 0;position:relative;box-shadow:0 0 20px #d0d0d0}
.gzhhide .gzhtitle{position:relative;font-size:17px;font-weight:700;color:#6c80a7;padding:6px 140px 0 40px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.gzhhide .gzhtitle .fa{position:absolute;left:0;font-size:35px;top:0}
.gzh-content{padding:20px 140px 15px 0;font-size:14px;color:#777}
.gzhbox{padding:0 140px 10px 0}
.gzhbox input{
    width:45%;
    border:none;
    color:#737373;
    font-size:13px;
height:35px;line-height:35px;background:#f2f2f2;border-radius:4px;
outline:none;float:left;padding:0 10px}
.gzhbox button{width:20%;margin-left:15%;
border:none;background:#3b8cff;color:#fff;padding:5px 0;font-size:14px;border-radius:5px}
.gzhhide .gzhcode{position:absolute;width:140px;height:140px;right:20px;top:50%;margin-top:-50px}
#vivideo{height:200px}
.gzhhide .gzhtitle i {font-style:normal;}

 

前端CSS样式二

/*纯代码实现WordPress文章设置隐藏内容公众号可见*/ 
.post_hide_box, .secret-password{background: none repeat scroll 0 0 #efe;color: #555;padding: 10px 0 10px 10px;border-radius: 5px;margin-bottom: 15px;overflow:hidden; clear:both;}
.post_hide_box .post-secret{font-size: 18px; line-height:20px; color:#e74c3c; margin:5px;}
.post_hide_box form{ margin:15px 0;}
.post_hide_box form span{ font-size:18px; font-weight:700;}
.post_hide_box .erweima{ margin-left:20px; margin-right:16px;}
.post_hide_box input[type=password]{ color: #9ba1a8; padding: 6px; background-color: #f6f6f6; border: 1px solid #e4e6e8; font-size: 12px;-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;}
.post_hide_box input[type=submit] {
    background: #F88C00;
    border: none;
    border: 2px solid;
    border-color: #F88C00;
    border-left: none;
    border-top: none;
    padding: 0px;
    width: 100px;
    height: 28px;
    color: #fff;
    outline: 0;
    border-radius: 0 0 2px 0;
    font-size: 16px;
}
.post_hide_box .details span{color:#e74c3c;}
.post_hide_box .post_secret_left {
    width: 75%;
    float: left;
}
.post_hide_box .post_secret_right{
    width: 25%;
    float: right;
}
.post_hide_box .post_secret_right_img {
    background-size: 100%;
    background-repeat: no-repeat;
    width: 150px;
    height: 150px;
    margin: 0 auto;
}
@media screen and (max-width:480px){
    .post_hide_box .post_secret_left {
        width: 100%;
        float: none;
    }
    .post_hide_box .post_secret_right{
        width: 100%;
        float: none;
    }
}

3 集成短代码

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

/*WordPress-文章部分内容关注微信公众号后可见**/
function gzh2v_secret_content($atts, $content=null){
    extract(shortcode_atts(array('key'=>null,'keyword'=>null), $atts));
    if(isset($_POST['secret_key']) && $_POST['secret_key']==$key){
        return '<div class="secret-password">'.$content.'</div>';
    } else {
        return '<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
        <div class="post_hide_box">
            <div class="post_secret_left">
                <div class="post-secret"><i class="fa fa-lock"></i> 此处内容已经被作者隐藏,请输入验证码查看内容</div>
                <form action="'.get_permalink().'" method="post"> 
                <span>验证码:</span><input id="pwbox" type="password" size="20" name="secret_key">
                <a class="a2" href="javascript:;"><input type="submit" value="提交" name="Submit"></a>
                </form>
                <div class="details">  请关注“菜鸟福利之家”官方微信公众号,回复关键字“<span><b>'.$keyword.'</b></span>”,获取验证码!<br>【注】用手机微信扫描右侧二维码即可关注“菜鸟福利之家”微信公众号。</div>
             </div>
             <div class="post_secret_right">
              <div class="post_secret_right_img" style="background-image: url(https://www.cnzzzj.com/wp-content/uploads/2020/09/wxgzh.jpg);"></div>
             </div>
        </div>';
    }
}
add_shortcode('gzh2v', 'gzh2v_secret_content');
// 后台文本编辑框中添加公众号隐藏简码按钮
function wpsites_add_gzh2v_quicktags() {
    if (wp_script_is('quicktags')){
?>
    <script type="text/javascript">
    QTags.addButton( 'gzh2v', '公众号隐藏', '<div class="secret-password">隐藏内容</div>',"" );
    </script>
<?php
    }
}
add_action( 'admin_print_footer_scripts', 'wpsites_add_gzh2v_quicktags' );

4 使用方法

4.1 在文章中使用短代码

4.2 微信公众号

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

还有一个方法是集成在编辑器按钮上:

在functions.php引入代码:

/*WordPress-文章部分内容关注微信公众号后可见引入*/
require_once get_template_directory() . '/customfun/custom-fun.php';

下载文件放到根目录,如果你改过functions.php,就加入上面的代码,没改过就覆盖。

演示截图:

2020.10.8更新管理员可见代码:

global $user_ID;
    if( $user_ID && current_user_can('level_10') ) : 
        return '<div class="secret-password">'.$content.'</div>';
    endif;

把上面的代码加入到下面

extract(shortcode_atts(array('key'=>null,'keyword'=>null), $atts));

下载地址:https://cnitzy.lanzoui.com/i3Qd6gx85he

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WordPress隐藏内容关注公众号可见引流插件可以帮助网站主将部分内容隐藏起来,只有用户关注公众号后才能够查看。这种方式可以有效地吸引更多的粉丝关注公众号,实现引流的目的。 WordPress隐藏内容关注公众号可见引流插件通常会提供一些简单的设置选项。通过这些选项,网站主可以选择哪些内容需要隐藏起来,例如文章的部分内容、下载链接、图片等。当用户访问网站时,被隐藏内容会显示一段文字或图片,提示用户需要关注公众号才能查看详细内容。 当用户点击关注按钮后,插件会验证用户关注情况,只有确认用户已经关注了公众号,插件才会自动显示隐藏内容给用户,否则仍然显示提示文字或图片。这样可以确保只有真正关注了公众号的用户才能够获取隐藏内容,有效地提高公众号的关注度。 WordPress隐藏内容关注公众号可见引流插件的使用方式相对简单,网站主只需要安装并激活插件,然后根据自己的需求进行设置即可。插件通常会提供一些样式和布局选项,可以根据网站的整体风格进行调整,使隐藏内容的提示效果更加美观。 总之,WordPress隐藏内容关注公众号可见引流插件是一种有趣且有效的方式,可以吸引更多用户关注公众号。通过隐藏部分内容来引发用户的好奇心,并通过关注公众号的方式获取详细内容,这种方式可以有效地增加粉丝数量并提升公众号的影响力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值