Cara Menambahkan Gambar CAPTCHA Anti Spam Dengan session_start() pada Form Komentar WordPress Tanpa Plugin hampir sama dengan cara memasang Gambar reCAPTCHA Anti Spam di Form Komentar WordPress Tanpa Plugin. Justru malah lebih mudah, karena kita tidak memerlukan API key.
Download terlebih dulu file captcha.zip, extract dan upload file "captcha.php" dan "monofont.ttf" ke dalam subdirektori images pada direktori wp-includes blog WordPress Anda.
Kemudian, tambahkan kode berikut pada form komentar pada theme WordPress Anda
<?php // if you're not logged in...
if (!is_user_logged_in()) {
?>
<p class="comment-form-user-info tight">
<input type="text" id="author-p<?php echo $post->ID; ?>" name="author" value="<?php echo $comment_author; ?>" size="22" />
<label for="author-pID; ?>"><?php _e('Name', 'carrington-blog'); if ($req) { echo ' <em>' , _e('(required)', 'carrington-blog'), '</em>'; } ?></label>
</p><!--/name-->
<p class="comment-form-user-info tight">
<input type="text" id="email-p<?php echo $post->ID; ?>" name="email" value="<?php echo $comment_author_email; ?>" size="22" />
<label for="email-p<?php echo $post->ID; ?>"><?php
_e('Email ', 'carrington-blog');
$req ? $email_note = __('(required, but never shared)', 'carrington-blog') : $email_note = __('(never shared)', 'carrington-blog');
echo ' <em>'.$email_note.'</em>';
?></label>
</p><!--/email-->
<p class="comment-form-user-info tight">
<input type="text" id="url-p<?php echo $post->ID; ?>" name="url" value="<?php echo $comment_author_url; ?>" size="22" />
<label title="" for="url-p<?php echo $post->ID; ?>"><?php _e('Web', 'carrington-blog'); ?></label>
</p><!--/url-->
<p class="comment-form-user-info tight">
<?php // Tambahkan kode ini
if (!$user_ID) : ?>
// fungsi reload gambar captcha
<script language="javascript" type="text/javascript">
function confirmRefresh() {
var okToRefresh = confirm("Do you really want to reload the captcha?");
if (okToRefresh) {
setTimeout("location.reload(true);",1000);
}
}
</script>
<a title="Reload Captcha Security Images" href="javascript:confirmRefresh();"><img alt="Captcha Security Images" src="http://alamat-WordPress-Anda.com/wp-includes/images/CaptchaSecurityImages.php?width=100&height=40&characters=5" /></a> <br />
<input type="text" name="security_code" id="security_code-p<?php echo $post->ID; ?>" tabindex="5" style="font-weight:bold" size="22" />
<label title="" for="security_code-p<?php echo $post->ID; ?>">Security Code</label>
<?php endif; ?>
</p><!--/security_code-->
<?php
}
?>
<p class="tight">
<input name="submit" type="submit" value="<?php _e('Post comment', 'carrington-blog'); ?>" />
Dan tambahkan file wp-comments-post.php yang berada di direktori blog WordPress Anda dengan code sebagai berikut
elseif ( !is_email($comment_author_email))
wp_die( __('<strong>ERROR</strong>: please enter a valid email address.') );
}
// Tambahkan kode berikut
if (!$user->ID) {
session_start();
if( $_SESSION['security_code'] != $_POST['security_code'] || empty($_SESSION['security_code'] ) ) {
wp_die( __('<strong>ERROR</strong>: Sorry, you have provided an invalid security code. Please go back and try again!') );
}
}
Jangan lupa, tambahkan kode penghancur sesi, untuk mengakhiri sesi dengan menambahkah kode
unset($_SESSION['security_code']); sebelum kode
exit;
Langkah terakhir, tambahkan kode berikut pada file .htaccess Anda
RewriteEngine on
RewriteRule captcha.jpg /wp-includes/images/CaptchaSecurityImages.php
Akhir kata, selamat mencoba dan salam.