wordpress免插件设置回复后可见

网上找了一圈,有用各种各样插件达成的,其中提到最多的是“Easy2Hide”,虽然它代码也蛮短的,但是“插件”二字总让我嫌弃,还是找纯代码的才能抚慰我轻微的强迫症。

WordPress是可以像论坛那样设置回复可见。代码也不需要多少

仿DZ效果:
步骤:

1.functions.php的?>前面添加代码:

function reply_to_read($atts, $content=null) {  
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read">提示: 此处内容需要<a href="#respond" title="评论本文">评论本文</a>后才能查看.</p>'), $atts));//notice默认值     
        $email = null;     
        $user_ID = (int) wp_get_current_user()->ID;     
        if ($user_ID > 0) {  
            $email = get_userdata($user_ID)->user_email;  
            $admin_email = "XXXX@email.tk"; //博主Email  
            if ($email == $admin_email) {//若是博主则直接显示内容  
                return '<p class="reply-to-read" style="background:no-repeat">'.$content.'</p>';  
            }  
        } else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {  
            $email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);  
        } else {  
            return strpos($notice,'提示: 此处内容需要')  ? $notice : '<p class="reply-to-read" title="此处内容需要评论本文后才能查看.">提示: '.$notice.'</p>';//未检测到评论信息(昵称、网站等)。不是博主,且未评论  
        }  
        if (empty($email)) {  
            return 'TEST CODE:111'.$notice;//无email  
        }  
        global $wpdb;  
        $post_id = get_the_ID();  
        $query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1";     
        if ($wpdb->get_results($query)) {  
            return '<p class="reply-to-read" style="background:no-repeat">'.do_shortcode($content).'</p>';//若已评论  
        } else {  
            return strpos($notice,'提示: 此处内容需要')  ? $notice : '<p class="reply-to-read" title="此处内容需要评论本文后才能查看.">提示: '.$notice.'</p>';//已评论其他,未评论此文章  
        }  
    }  
    add_shortcode('reply', 'reply_to_read');

 

说明:使用前将第10行的email地址改为自己的

我这稍微美化些

2.style.CSS中添加:

.reply-to-read {
overflow: hidden;
margin: 10px 0;
padding: 8px 8px 8px 24px;
border: 1px dashed hsl(0, 100%, 80%);
background: hsl(0, 100%, 100%) url( 链接) no-repeat 6px 50%;
font-size: 12px;
zoom: 1;
}

 

说明:

3.使用的时候输入短代码:(下面的【】分别替换为[])

【reply】隐藏的内容【/reply】

或者

【reply notice=自定义信息】隐藏的内容【/reply】

ps:嫌每次输reply短代码麻烦,可以参考>>

WordPress 3.5.1添加后台编辑器按钮

来添加按钮,方便多了。

这里的话在my-quicktags.js中添加:(下面的【】分别替换为[])
QTags.addButton( ‘reply’, ‘reply回复可见’, “n【reply notice=】”, “【/reply】n” );

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
上一篇

WordPress 微信支付接口 商户支付密钥key的生成与设置

2019-7-2 20:52:32

下一篇

wordpress判断if(is_home())是否首页的功能无效?

2020-3-6 20:49:10