短标签隐加密部分博文内容-WordPress开发

隐藏部分博文内容,或者让它输入密码可见。

自定义短代码来隐藏部分博文内容

wordpress隐藏部分文章内容登陆可见

来源于互联网

在functions.php文件添加以下代码:

add_shortcode('hide','loginvisible');
function loginvisible($atts,$content=null){
	if(is_user_logged_in() && !is_null($content) && !is_feed())
	return $content;
	return '';
}

编辑文章时,使用短码包围要隐藏的内容(文本模式下),如:

[hide]
登陆才可以看到的内容
[/hide]

给博文添加密码,需输入密码才可访问。

将以下代码复制到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: auto;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文件中

使用:[ssecret key="输入密码"][/ssecret]

效果如下:

短标签隐加密部分博文内容-WordPress开发
随记

做个人博客有啥用?

2019-7-29 18:02:20

国内主题

Pure - Pjax极简WordPress主题

2019-1-3 15:27:14

⚠️
Npcink上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。
无意侵害您的权益,请发送邮件至 1355471563#qq.com 或点击右侧 私信:Muze 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索