RedJumpsuit

jobberBase custom development and support

reCaptcha on Contact Form

contact-usone of our website has started getting spam messages from bots sending through the contact form, and obviously the easiest way to minimize (if not all together stop) is to just enable reCaptcha on the contact form. by default, jobberBase only has reCaptcha enabled when posting a new job and applying for a job. so here’s how you can implement reCaptcha on the Contact Form as well.

open page_page.php on the root folder and add this before the first line:

$smarty->assign('the_captcha', recaptcha_get_html(CAPTCHA_PUBLIC_KEY));
$smarty->assign('ENABLE_RECAPTCHA', ENABLE_RECAPTCHA);

on the same file, before these lines:

if ($contact_name == '')
{
	$errors['contact_name'] = $translations['contact']['name_error'];
}

add this:

// validation
if (ENABLE_RECAPTCHA)
{
	$resp = recaptcha_check_answer(CAPTCHA_PRIVATE_KEY,
	$_SERVER["REMOTE_ADDR"], 
	$_POST["recaptcha_challenge_field"],
	$_POST["recaptcha_response_field"]);
 
	if (!$resp->is_valid) 
	{
		$errors['captcha'] = $translations['captcha']['captcha_error'];
	}
}

that should take care of generating the reCaptcha field. and then next, open /_includes/default/page.tpl and before the submit field

<input type="submit" name="submit" id="submit" value="{$translations.contact.submit}" />

add this block:

{if $ENABLE_RECAPTCHA}
<label>Anti-Spam:</label><br />
	{literal}
	<script type="text/javascript">
	  var RecaptchaOptions = {
		theme : 'white',
		tabindex : 9
	  };
	</script>
	{/literal}
	{$the_captcha} <span class="validation-error">{if $smarty.session.apply_errors.captcha}
	<img src="{$BASE_URL}_templates/{$THEME}/img/icon-delete.png" alt="" /> {$errors.captcha}{/if}</span>
	<br /><br />
{/if}

Leave a Response