wordpress自带的有整篇文章加密功能,但是有时候会遇到部分内容加密的需求,要怎么实现呢?下面给出解决方法,效果如图:
以下代码加入到主题的functions.php文件中,本文以wordpress自带主题为例:
- /**文章部分内容加密开始*/
- function password_post($atts, $content=null){
- extract(shortcode_atts(array('key'=>null), $atts));
- if(isset($_POST['password_key']) && $_POST['password_key']==$key){
- return '
- <div class="jm-content">加密的内容:'.$content.'</div>
- ';
- }else{
- return '
- <div class="jm-post">
- <span style="background-color: #5b5be8; font-size: 2em;">**相关资源**</span>
- <form class="jm-post-form" action="'.get_permalink().'" method="post">
- 输入密码查看加密内容:<input type="password" id="password_key" name="password_key" size="20"/>
- <input type="submit" value="提交" />
- </form>
- <p>**下载提示**</p>
- <p>1)这部分内容需要下载密码</p>
- <p>2)在输入框输入指定密码</p>
- <p>3)然后就可以访问了</p>
- </div>
- ';
- }
- }
- add_shortcode('pwd_post','password_post');
- /**文章部分内容加密结束*/
样式文件:
- /**文章部分内容加密**/
- .jm-post {
- background: #e8f5f5;
- padding: 10px;
- font-size: 13px;
- line-height: 24px;
- margin-top: 8px;
- margin-bottom: 8px;
- }
- .jm-content {
- margin: 20px 0;
- padding: 20px;
- background: #f8f8f8;
- }
- .jm-post-form {
- margin: 15px 0;
- padding: 15px 5px;
- border-radius: 2px;
- border: 1px solid #ddd;
- }
- .jm-post-form input[type="password"] {
- width: 40%;
- height: 38px;
- line-height: 30px;
- background: #fff;
- padding: 5px;
- border-radius: 2px;
- border: 1px solid #ddd;
- -webkit-appearance: none;
- }
- .jm-post-form input[type="submit"] {
- width: 80px;
- line-height: 25px;
- padding: 5px;
- cursor: pointer;
- border-radius: 2px;
- border: 1px solid #ddd;
- -webkit-appearance: none;
- }
- .jm-post input[type="submit"]:hover {
- background: #3690cf;
- color: #fff !important;
- border: 1px solid #3690cf;
- }
使用
在文章需要加密的地方输入[pwd_post key=密码]加密内容[/pwd_post]
完。

微信小程序
互联网开发,终身学习者,欢迎您的关注!