PHP: Modify WP No-Bot Question with IP Geolocation

I’ve decided to chop away the default routine of WP No-Bot Question to add another spin to it.

It’s a very simple procedure. I use CloudFlare so this allows me to use:

$some_var = $_SERVER["HTTP_CF_IPCOUNTRY"];
echo '<html><body><p>'.$some_var.'</p></body></html>'; 

In this case it would merely output “DK” (my proxy is Danish, ha ha).

Let’s begin. Fire up wp_nobot_question.php in Bluefish or whatever editor you prefer, and jump to line #109, and replace the entire function with this one:

function wp_nobot_question_filter($x) {
	if( current_user_can('editor') || current_user_can('administrator') ||
	    ( /* Is registration? */!is_array($x) && !wp_nobot_question_get_option('registration') )||
	    $x['comment_type'] == 'pingback' || $x['comment_type'] == 'trackback' ||
	    !wp_nobot_question_get_option('enable') ) {
		return $x;
	if(!array_key_exists('wp_nobot_answer',$_POST) || trim($_POST['wp_nobot_answer']) == '') {
		wp_redirect( '', 302 ); exit;
		if(trim($_POST['wp_nobot_answer']) == $answer) return $x;
	wp_redirect( '', 302 ); exit;

You can naturally replace the wp_redirect(); instances with whatever you desire, but I prefer to send all faulty requests to my honeypot.

Next, locate lines #96 to #107. Replace the entire block with this bit:

<p class="comment-form-wp_nobot_question">
	<label for="wp_nobot_answer"><?php echo wp_nobot_question_get_option('question'); ?> (<?php _e('Shhhh, your country code is:','wp_nobot_question'); ?> <?php echo $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; ?>)</label>
		<?php if($context == 'registration') { ?> tabindex="25" <?php }; ?>

I could’ve replaced the wp_nobot_question_get_option(‘question’); part in the code above, but decided not to. I’m not sure why, since it takes up useless queries to my MySQL server. So you can either replace that part with text of your choice, or keep it. If you decide to keep it you must navigate to your WP-admin area and go into the plugin settings of WP No-Bot Question and modify the question like so:

What’s your country code?

Well, that’s all really. Don’t forget to enable IP Geolocation in CloudFlare

[EDIT: I have deleted the wp_nobot_question_get_option(‘question’); bit from my own modified WP No-Bot plugin. It’s useless to have it, really.]

Leave a Reply

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