前言说明

很多同学在访问别人网站时,应该都会看到:“该帖子需要回复后才能看到”、“本链接需要回复才能下载”等等的这类效果,那么今天我们就分享一个适用于WordPress博客的代码,这样就可以在我们的WordPress网站中实现同类的功能了,这样对于网站访客虽然有点麻烦,但对于网站本身的流量或者用户粘度还是有着很大的帮助的,这样同时也变相的增强了我们网站的权重也有利于SEO!

给WordPress添加一个评论后可见功能插图

教程代码

1、首先我们将下面的代码,加到自己使用主题的“functions.php” 中:

//短代码之评论可见   
function reply_to_read($atts, $content=null) {
	extract(shortcode_atts(array("notice" => '<p class="reply-to-read"><span style="color:red; font-size=13px;">温馨提示:</span>此处内容需要<a href="#respond" title="评论本文">评论本文</a>后才能查看.</p>'), $atts));
	$email = null;
	$user_ID = (int) wp_get_current_user()->ID;
	if ($user_ID > 0) {
		$email = get_userdata($user_ID)->user_email;
		//对博主直接显示内容   
		$admin_email = "963020001@qq.com"; //管理员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');

2、在发布文章时,调用时我们需要的简代码即可:

[reply]评论可见的内容[/reply]

或者带自定义的方法

[reply notice="自定义提醒回复内容"]自定义提醒回复内容[/reply]

注意:在上述代码中把邮箱改为自己管理员的绑定邮箱,这样在发布文章后,管理员就可以直接显示,而不需要回复才能查看!

1.使用本站下载的源码仅限于个人学习和非商业用途。
2.禁止将本站下载的源码用于搭建或支持任何违法、淫秽、暴力或侵犯他人合法权益的网站或应用。
3.使用本站下载的源码需遵守国家法律法规及相关规定,不得从事任何违法活动。
4.如若本站内容侵犯了原著者的合法权益,请联系我们进行处理。