新版 WordPress 评论数字验证

在之前的版本上做了一点修改,使其除了支持 + 运算以外还支持 – 和 x 运算。改动不是很大的,就是做一个记录。

wp-comments-post.php
修改 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_method = (isset($_POST['method'])) ? trim($_POST['method']) : null;
$comment_result	= (isset($_POST['result'])) ? trim($_POST['result']) : 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@heihaier.org 
 */
if(!$user->ID)		// guest user only
{
	switch($comment_method)
	{
	case 0:	// +
		if((($comment_numa + $comment_numb) != $comment_result) || empty($comment_result))
		  wp_die(__('Error: please type the correct results of the math problems.'));
		break;
	case 1: // -
		if((($comment_numa - $comment_numb) != $comment_result) || empty($comment_result))
		  wp_die(__('Error: please type the correct results of the math problems.'));
		break;
	case 2: // *
		if((($comment_numa * $comment_numb) != $comment_result) || empty($comment_result))
		  wp_die(__('Error: please type the correct results of the math problems.'));
		break;
	}
}


comments.php
修改当前主题包中的 comments.php 程序, 增加如下内容。

<!-- Comment Math Checker -->
<!-- Heihaier - admin@heiher.info -->
<?php if (!$user_ID) : ?>		<!-- guest user only -->
	<?php $numa = rand(0, 9); $numb = rand(0, 9); $method = rand(0, 2); ?>
	<?php echo $numa; ?> <?php switch($method) {case 0: echo "+"; break; case 1: echo "-"; break; case 2: echo "x"; break;} ?> <?php echo $numb; ?> = <input type="text" name="result" id="result" class="textfiled" maxlength="3" size="3" 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; ?>" />
	<input type="hidden" name="method" value="<?php echo $method; ?>" />
<?php endif; ?>

Over!

3 thoughts on “新版 WordPress 评论数字验证”

Leave a Reply

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