实现WordPress文章输入密码可见隐藏内容

看到某老司机开车,所有的车牌必须输入密码才可见。觉得蛮好玩的!
现在黑鸟站长就带你使用代码实现文章输入密码后才可见隐藏内容,手扶好,要加速了。

步骤

将以下代码复制到function.php中

/**
* 建立短代码-加载密码可见
*/

function e_secret($atts, $content=null){
    extract(shortcode_atts(array('key'=>null), $atts));
    if(isset($_POST['e_secret_key']) && $_POST['e_secret_key']==$key){
        return '
            <div class="e-secret" id="eallsecret">'.$content.'</div>
            ';
    }
    else{
        return '
            <form class="e-secret" action="'.get_permalink().'#eallsecret'.'" method="post" name="e-secret"><label class="e_secret_lab">输入密码查看加密内容:</label><input type="password" name="e_secret_key" class="euc-y-i" maxlength="50"><input type="submit" class="euc-y-s" value="确定">
            <div class="euc-clear"></div>
            </form>
            ';
    }
}
add_shortcode('ssecret','e_secret');

/**
* 样式-加载密码可见
*/

function secret_css() {
    global $post,$posts;
    foreach ($posts as $post) {
        if ( has_shortcode( $post->post_content, 'ssecret') ){
                echo '<style type="text/css">.e-secret {margin: 20px 0;padding: 20px;background: #f8f8f8;overflow: auto;}.e-secret input.euc-y-i[type="password"] {float: left;bacground: #fff;width: 100%;line-height: 36px;margin-top: 5px;border-radius: 3px;}.e-secret input.euc-y-s[type="submit"] {float: right;margin-top: -47px;width: 30%;margin-right: 1px;border-radius: 0 3px 3px 0;}input.euc-y-s[type="submit"]{background-color:#FF0016;color:#fff;font-size:21px;box-shadow:none;-webkit-transition: .4s;-moz-transition: .4s;-o-transition: .4s;transition:.4s;-webkit-backface-visibility:hidden;position:relative;cursor:pointer;padding: 13px 20px;text-align: center;border-radius: 50px;-webkit-box-shadow: none;    -moz-box-shadow: none;    box-shadow: none;border: 0;height: auto;outline: medium;line-height: 20px;margin: 0;}input.euc-y-s[type="submit"]:hover{background-color:#CE0416;}input.euc-y-i[type="text"],input.euc-y-i[type="password"]{border:1px solid #F2EFEF;color:#777;display:block;background: #FCFCFC;font-size:18px;transition:all .5s ease 0;outline:0;box-sizing:border-box;-webkit-border-radius:25px;-moz-border-radius:25px;border-radius:25px;padding:5px 16px;    margin: 0;height: auto;line-height: 30px;}input.euc-y-i[type="text"]:hover,input.euc-y-i[type="password"]:hover{border:1px solid #56b4ef;box-shadow:0 0 4px #56b4ef</style>';
        }
    }
}
add_action('wp_head', 'secret_css');

/**
* 编辑器密码可见按钮-加载密码可见
*/

function mmkj_tags($mce_settings) {
    ?>
    <script type="text/javascript">
        QTags.addButton( 'mimakejian', '密码可见', '【ssecret key="输入密码"】', '【/ssecret】' );
    </script>
    <?php
}
add_action('after_wp_tiny_mce', 'mmkj_tags');

以上”【 】”请使用”[ ]”替换
以上“function secret_css()”部分亦可以把CSS代码放入style.css文件中

实例

Demo敲门码:123456


网络上类似版本的代码很多都存在CSS布局溢出等问题,本次解决了溢出问题,并且首创在提交正确的密码刷新页面后直接跳转到隐藏部分的内容。
OK,完成,就是这个样子。

回复 佛系软件 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

  1. 佛系软件 说道:

    或者说换一个密码所有的密码全部换,怎么搞啊

  2. 佛系软件 说道:

    那怎么把密码改成验证码呢

  3. Terry Terry 说道:

    请问添加完了,怎么调用呢?不是很明白,把哪部分改成密码才可以使用

    1. 黑鸟 说道:

      旧的文章编辑器有短代码按钮,最新的古藤保编辑器没有

  4. sci666 说道:

    折腾了好久都没弄好,终于看到你了博客才解决了!感谢!

    希望再出一个 评论后可见隐藏内容 的教程,网上很多代码都不兼容php7.0

  5. BudgetVM VPS 说道:

    朋友 交换链接吗

    1. 鬼手六 说道:

      先友,后链 。

  6. 夏天烤洋芋 说道:

    :mrgreen: 这个方法挺不错的!

    1. 鬼手六 说道:

      给访客一个出其不意的内容, :cool: 又一个恶搞利器..