修改主题functions文件添加以下代码:
function verification_fields($fields) {
$num_o = rand(0, 99);
$num_t = rand(0, 99);
$fields['verification'] = '<input name="num_o" value="' . $num_o . '" type="hidden" />' .
'<input name="num_t" value="' . $num_t . '" type="hidden" />' .
'<p class="verification-code"><label for="verification-code">验证:</label>' .
$num_o . ' + ' . $num_t . ' = ' . '<input id="verification-code" name="pcodes" type="text" value="" size="4" /></p>';
return $fields;
}
add_filter('comment_form_default_fields', 'verification_fields');
function verification_code() {
if ( ! $user->ID ) {
$num_o = trim($_POST['num_o']);
$num_t = trim($_POST['num_t']);
$pcodes = trim($_POST['pcodes']);
if( $pcodes != $num_o + $num_t ) {
wp_die( __('错误提示:请输入正确的验证码。') );
}
}
}
add_filter('pre_comment_on_post', 'verification_code');