WordPress纯代码添加内容回复可见功能
本文最后更新于 504 天前,文中的信息可能已经有所变化。如有误,请留言反馈。

WordPress纯代码添加内容回复可见功能,和之前的《WordPress添加内容需输入密码可见功能》一样,是为了提高人气而添加的功能;将一些下载链接等内容设置为评论后显示,但可能会多很多没有意义的评论,按需添加。

以下是WordPress纯代码添加内容回复可见功能的代码,共三步:

  • 在主题的functions.php文件添加以下代码:
// 部分内容评论可见
add_filter('the_content', 'hide');
add_filter('comment_text','hide');
function hide($content) {
if (preg_match_all('/<!--hide start{?([\s\S]*?)}?-->([\s\S]*?)<!--hide end-->/i', $content, $matches)) {
$params = $matches[1][0];
$defaults = array('reply_to_this' => 'false');
$params = wp_parse_args($params, $defaults);
$stats = 'hide';

if ($params['reply_to_this'] == 'true') {
global $current_user;
get_currentuserinfo();

if ($current_user->ID) {
$email = $current_user->user_email;
} else if (isset($_COOKIE['comment_author_email_'.COOKIEHASH])) {
$email = $_COOKIE['comment_author_email_'.COOKIEHASH];
}

$ereg = "^[_\.a-z0-9]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,5}$";
if (eregi($ereg, $email)) {
global $wpdb;
global $id;
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");
if ($comments) {
$stats = 'show';
}
}
$tip = __('<span class="vihide">抱歉,隐藏内容 <a href="#comments">回复</a> 后刷新可见</span>', 'hide');
} else {
if (isset($_COOKIE['comment_author_'.COOKIEHASH]) or current_user_can('level_0')) {
$stats = 'show';
}
$tip = __();
}

$hide_notice = $tip;
if ($stats == 'show') {
$content = str_replace($matches[0], $matches[2], $content);
} else {
$content = str_replace($matches[0], $hide_notice, $content);
}
}

return $content;
}
add_action('admin_footer', 'hide_footer_admin');
  • 将评论可见添加编辑器快捷按钮,将以下代码加入主题的functions.php文件:
// 添加编辑器按钮 - 回复可见
function reply_view_tags($mce_settings) {
?>
<script type="text/javascript">
QTags.addButton( 'qiuzhuti_reply_view', '回复可见', '<!--hide start{reply_to_this=true}-->', '<!--hide end-->' );
</script>
<?php
}
add_action('after_wp_tiny_mce', 'reply_view_tags');
  • 加入CSS,将以下代码加入主题的CSS文件(也可自行修改):
/*回复可见*/
.vihide{display:inline-block;text-align:center;border: 2px dashed #ff6666;padding:8px;margin:10px auto;color:#FF6666;width:100%;}
.vihide a{color:#04a1ef}
.vihide a:hover{color:#4ec6c8}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇