WordPress纯代码设置添加文章部分内容密码保护功能 加密内容支持短代码

WordPress纯代码设置添加文章部分内容密码保护功能 加密内容支持短代码

修改教程

1,将以下代码添加到主题的 functions.php 文件中;

 

// 输入密码查看文章内容
function password_protected_post($atts, $content=null){
    extract(shortcode_atts(array(‘key’=>null), $atts));
    if(isset($_POST[‘password_key’]) && $_POST[‘password_key’]==$key){
        return
<div class=”password_protected_post_content”>’.do_shortcode($content).‘</div>
;
    }elseif(isset($_POST[‘password_key’]) && $_POST[‘password_key’]!=$key){
        return
<script>
alert(“抱歉,密码错误,请仔细核对密码后重试!”);
window.location.href=“‘.get_permalink().'”;
</script>
;
}else{
        return
<form class=”password_protected_post_form” action=”‘.get_permalink().‘” method=”post”>
<input type=”password” id=”password_key” name=”password_key” size=”20″ placeholder=”请输入密码查看隐藏内容,未知请添加TG群/TG频道获取密码”/>
<input type=”submit” value=”确    定” />
</form>
;
    }
}
add_shortcode(‘pwd_protected_post’,‘password_protected_post’);
// 输入密码查看文章内容快捷按钮
// https://saodaye.com/wordpress/skill/1473.html
function appthemes_add_pwd_protected_post() {
if (wp_script_is(‘quicktags’)){
?>
    <script type=“text/javascript”>
        QTags.addButton( ‘pwd_protected_post’, ‘文章密码保护’, ‘[pwd_protected_post key=”保护密码”]’,‘[/pwd_protected_post]’ );
    </script>
<?php
    }
}
add_action(‘admin_print_footer_scripts’, ‘appthemes_add_pwd_protected_post’ );
2,将下面的样式代码添加到style.css文件中;
/** 输入密码查看文章内容 */
.password_protected_post_content{
margin: 20px 0;
    padding: 0px;
//*    background: #f8f8f8; 背景 */
}
.password_protected_post_form{
margin: 20px 0;
height:52px;
fontsize:24px;
textalign: center;
}
.password_protected_post_form [type=“password”]{
float: left;
    width: 80%;
height: 100%;
display: block;
padding: 5px 16px;
lineheight: 36px;
color: #777;
background: #FCFCFC;
border: 3px solid #f3f3f3;
borderright: 0px;
borderradius: 3px 0 0 3px;
boxsizing: borderbox;
}
.password_protected_post_form [type=“submit”]{
float: right;
    width: 20%;
height: 100%;
display: block;
padding: 5px 16px;
lineheight: 36px;
color: #fff;
background: #5a6a7a;
border: 3px solid #5a6a7a;
borderleft: 0px;
borderradius: 0 3px 3px 0;
boxsizing: borderbox;
cursor: pointer;
}
@media (maxwidth:560px){
.password_protected_post_form{
height:36px;
fontsize:14px;
textalign: center;
}
.password_protected_post_form [type=“password”]{
padding: 3px 9px;
lineheight: 24px;
}
.password_protected_post_form [type=“submit”]{
padding: 3px 9px;
lineheight: 24px;
}
}
3,编辑器切换到文本模式即可看到 文章密码 保护,点击2次即可实现加密;
点赞

发表评论

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