wordpress部分内容加密

wordpress有文章加密的功能,但是只能加密整篇文章,如果只想加密文章的部分内容,海天在网上找了一段代码,可以通过在文章中使用短代码来实现部分内容加密。

首先将以下加密函数代码,添加到function.php文件里

function secret($atts, $content=null) {
    extract(shortcode_atts(array('key' => null, 'tip' => null), $atts));
    if (
        isset($_SESSION[hash('md5', $key)]) ||
        (isset($_POST['secret_key']) && $_POST['secret_key'] == $key)
    ) {
        $_SESSION[hash('md5', $key)] = $key;
        return '<div class="secret">' . $content . '</div>';
    } else {
        if (isset($_POST['secret_key'])) {
            $tip = '<p class="secret_tip">密码输入错误!</p>' . $tip;
        }
        return '<form class="secret" action="'.get_permalink().'" method="post" name="secret"><label>输入密码查看加密内容:</label><input type="password" name="secret_key" class="euci" maxlength="50"><input type="submit" class="eucs" value="确定">
      <div class="eucc"></div></form>' . '<p class="secret_tip">' . $tip . '</p>';
    }
}
add_shortcode('jiami', 'secret');

其中secret($atts, $content=null)函数实现了加密功能,$atts包含设置的参数,这里默认包含两个参数一个是key, 用来存储加密密码;另一个是tip,用来存储提示信息,提示信息显示在密码输入框的下面,可以自行修改提示内容。

函数使用session记录密码,以浏览器来辨认用户的身份。

然后是加密模块的样式,网上有个现成的,感觉样式还不错,也和博客主题搭配,一起分享给大家使用,将下列样式代码,加到css文件里就可以了

.secret {
 margin: 20px 0;
 padding: 20px;
 background: #f8f8f8;
}
.secret input.euci[type="password"] {
 float: left;
 background: #fff;
 width: 100%;
 line-height: 36px;
 margin-top: 5px;
 border-radius: 3px;
}
.secret input.eucs[type="submit"] {
 float: right;
 margin-top: -47px;
 width: 30%;
 margin-right: 1px;
 border-radius: 0 3px 3px 0;
}
input.eucs[type="submit"]{background-color:#3498db;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.eucs[type="submit"]:hover{background-color:#3274ff;}
input.euci[type="text"],input.euci[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.euci[type="text"]:hover,input.euci[type="password"]:hover{border:1px solid #56b4ef;box-shadow:0 0 4px #56b4ef;}

最后是使用方法,只要将需要加密的内容用短代码包围起来,比如: