WordPress隐藏内容回复(评论)可见功能,纯代码实现,非插件。
使用起来很方便,不需要手动输入代码,在编辑文章的时候,切换至“文本”模式下,有一个“回复可见”按钮,点击输入隐藏内容即可。
请将如下代码放入主题的function.php中:
//部分内容评论可见
function reply_to_read($atts, $content = null) {
extract(shortcode_atts(array(
"notice" => '<div class="reply">隐藏内容,回复可见“<a href="' . get_permalink() . '#respond" title="回复可见">马上回复</a>”后“<a href="javascript:window.location.reload();" title="刷新页面">刷新页面</a>”即可查看隐藏内容!</div>'
) , $atts));
$email = null;
$user_ID = (int)wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = get_bloginfo('admin_email');
if ($email == $admin_email) {
return $content;
}
} else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
$email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
} else {
return $notice;
}
if (empty($email)) {
return $notice;
}
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 do_shortcode($content);
} else {
return $notice;
}
}
add_shortcode('reply', 'reply_to_read');
//添加HTML编辑器自定义快捷标签按钮
add_action('after_wp_tiny_mce', 'bolo_after_wp_tiny_mce');
function bolo_after_wp_tiny_mce($mce_settings) {
?>
<script type="text/javascript">
QTags.addButton( 'reply', '回复可见', '[@reply]\n\n[/reply]\n', "" );
</script>
<?php
}
以上代码其中[@reply]中的@去掉
CSS
/*回复可见*/
.reply {
padding: 10px;
border: 1px #49aff9 dashed;
background-color: #fafafa;
color: #f40000;
text-align: center;
font-size: 14px;
}