wordpress代码实现内容回复可见功能

wordpress代码实现内容回复可见功能

作者 : 资源客 发布时间: 2019-12-9

今天给我的博客加了文章内容回复可见的功能,代码版的,网上有些都已经失效了,我特意重新整理了一份出来,并且在后台编辑器也加了快捷按钮,下面言归正传,总共分为 3 步。

1. 在 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');

2.在 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');

3. 加入 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}

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » wordpress代码实现内容回复可见功能