WordPress 评论数学验证的简单实现

谈到 WordPress 的垃圾评论,我已经无语了。下面给出一个 WordPress 评论数学验证的简单实现方法。希望能够解决由垃圾评论给你带来的烦恼。

1. 编辑 WordPress 安装目录的 wp-comments-post.php 文件,修改下面列出的两处:

$comment_author       = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
$comment_author_email = ( isset($_POST['email']) )   ? trim($_POST['email']) : null;
$comment_author_url   = ( isset($_POST['url']) )     ? trim($_POST['url']) : null;
$comment_content      = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
// 在其后加入如下代码
/* Comment Math Checker
 * Heihaier - admin@heiher.info 
 */
$comment_numa	= (isset($_POST['numa'])) ? trim($_POST['numa']) : null;
$comment_numb	= (isset($_POST['numb'])) ? trim($_POST['numb']) : null;
$comment_sumab	= (isset($_POST['sumab'])) ? trim($_POST['sumab']) : null;
if ( get_option('require_name_email') && !$user->ID ) {
	if ( 6 > strlen($comment_author_email) || '' == $comment_author )
		wp_die( __('Error: please fill the required fields (name, email).') );
	elseif ( !is_email($comment_author_email))
		wp_die( __('Error: please enter a valid email address.') );
}
// 在其后加入如下代码
/* Comment Math Checker
 * Heihaier - admin@heiher.info 
 */
if(!$user->ID)		// guest user only
{
	if((($comment_numa + $comment_numb) != $comment_sumab) || empty($comment_sumab))
		wp_die(__('Error: please type the correct results of the math problems.'));
}


2. 编辑当前主题程序的 comments.php 文件,例如我使用 inove 主题就是 wp-content/themes/inove/comments.php

<!-- comment submit and rss -->
<div id="submitbox">
	<a class="feed" href="<?php bloginfo('comments_rss2_url'); ?>"><?php _e('Subscribe to comments feed', 'inove'); ?></a>
	<div class="submitbutton">
		<!-- 其后加入如下代码 -->
		<!-- Comment Math Checker -->
		<!-- Heihaier - admin@heiher.info -->
		<?php if (!$user_ID) : ?>		<!-- guest user only -->
			<?php $numa = rand(0, 5); $numb = rand(3, 9); ?>
			<?php echo $numa; ?> + <?php echo $numb; ?> = <input type="text" name="sumab" id="sumab" class="textfiled" size="2" tabindex="5" style="width:20px;" />
			<?php if($req)_e('(required)', 'inove'); ?>  <!-- 根据当前主题修改 -->
			<input type="hidden" name="numa" value="<?php echo $numa; ?>" />
			<input type="hidden" name="numb" value="<?php echo $numb; ?>" />
		<?php endif; ?>
 
		<input name="submit" type="submit" id="submit" class="button" tabindex="5" value="<?php _e('Submit Comment', 'inove'); ?>" />
	</div>
	<?php if (function_exists('highslide_emoticons')) : ?>
		<div id="emoticon"><?php highslide_emoticons(); ?></div>
	<?php endif; ?>
	<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
	<div class="fixed"></div>
</div>

Over!

2 thoughts on “WordPress 评论数学验证的简单实现”

Leave a Reply

Your email address will not be published. Required fields are marked *