If you want to use core captcha functionality in contact us form you need to develop a module in your local directory.
Here “Compny” is my namespace and “Captcha” is module name
First of all create module config file Compny_Captcha.xml in app/etc/modules/
<?xml version=”1.0″?>
<config>
<modules>
<Compny_Captcha>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Captcha/>
</depends>
</Compny_Captcha>
</modules>
</config>
create module config.xml file in app/code/local/Compny/Captcha/etc/config.xml
<?xml version=”1.0″?>
<config>
<modules>
<Compny_Captcha>
<version>1.8.0.0.0</version>
</Compny_Captcha>
</modules>
<global>
<models>
<captcha>
<rewrite>
<zend>Compny_Captcha_Model_Zend</zend>
</rewrite>
</captcha>
</models>
</global>
<default>
<captcha translate=”label”>
<frontend>
<areas>
<contact_us>
<label>Contact us Form</label>
</contact_us>
</areas>
</frontend>
</captcha>
<customer>
<captcha>
<always_for>
<contact_us>1</contact_us>
</always_for>
</captcha>
</customer>
</default>
<frontend>
<routers>
<contacts>
<args>
<modules>
<Compny_captcha before=”Mage_Contacts”>Compny_Captcha</Compny_captcha>
</modules>
</args>
</contacts>
</routers>
</frontend>
</config>
Here You can override core captcha model because captcha is not appear after customer login if you want to display captcha after customer login so you need to override Mage_Captcha_Model_Zend file _isUserAuth() function
create Zend.php file in app/code/local/Compny/Captcha/Model/Zend.php
<?php
class Compny_Captcha_Model_Zend extends Mage_Captcha_Model_Zend
{
protected function _isUserAuth()
{
//die(‘function called’);
/* return Mage::app()->getStore()->isAdmin()
? Mage::getSingleton(‘admin/session’)->isLoggedIn()
: Mage::getSingleton(‘customer/session’)->isLoggedIn();*/
}
}
In this function you need to only comment function code.
Now create frontend layout you need to edit contacts.xml in app/design/frontend/default/default/layout/contacts.xml
Here you need to add captcha block under
<reference name=”content”>
<block type=”core/template” name=”contactForm” template=”contacts/form.phtml”>
<block type=”core/text_list” name=”form.additional.info”>
<block type=”captcha/captcha” name=”captcha”>
<reference name=”head”>
<action method=”addJs”><file>mage/captcha.js</file></action>
</reference>
<action method=”setFormId”><formId>contact_us</formId></action>
<action method=”setImgWidth”><width>230</width></action>
<action method=”setImgHeight”><width>50</width></action>
</block>
</block>
</block>
</reference>
After that you need to call block in frontend form.phtml file in app/design/frontend/default/default/template/contacts/form.phtml
you just put the below line before completing </ul>
tag
<?php echo $this->getChildHtml(‘form.additional.info’); ?>
Now you need to override contact us controller create file in app/code/local/Compny/Captcha/controllers/IndexController.php
<?php
require_once(Mage::getModuleDir(‘controllers’,’Mage_Contacts’).DS.’IndexController.php’);
class Compny_Captcha_IndexController extends Mage_Contacts_IndexController
{
public function postAction()
{
$post = $this->getRequest()->getPost();
if ( $post ) {
$translate = Mage::getSingleton(‘core/translate’);
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($post);
$error = false;
if (!Zend_Validate::is(trim($post['name']) , ‘NotEmpty’)) {
$error = true;
}
if (!Zend_Validate::is(trim($post['comment']) , ‘NotEmpty’)) {
$error = true;
}
if (!Zend_Validate::is(trim($post['email']), ‘EmailAddress’)) {
$error = true;
}
if (Zend_Validate::is(trim($post['hideit']), ‘NotEmpty’)) {
$error = true;
}
$formId =’contact_us’;
$captchaModel = Mage::helper(‘captcha’)->getCaptcha($formId);
if ($captchaModel->isRequired()) {
if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId))) {
Mage::getSingleton(‘customer/session’)->addError(Mage::helper(‘captcha’)->__(‘Incorrect CAPTCHA.’));
$this->setFlag(”, Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
Mage::getSingleton(‘customer/session’)->setCustomerFormData($this->getRequest()->getPost());
$this->getResponse()->setRedirect(Mage::getUrl(‘*/*/’));
return;
}
}
if ($error) {
throw new Exception();
}
$mailTemplate = Mage::getModel(‘core/email_template’);
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array(‘area’ => ‘frontend’))
->setReplyTo($post['email'])
->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
null,
array(‘data’ => $postObject)
);
if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}
$translate->setTranslateInline(true);
Mage::getSingleton(‘customer/session’)->addSuccess(Mage::helper(‘contacts’)->__(‘Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.’));
$this->_redirect(‘*/*/’);
return;
} catch (Exception $e) {
$translate->setTranslateInline(true);
Mage::getSingleton(‘customer/session’)->addError(Mage::helper(‘contacts’)->__(‘Unable to submit your request. Please, try again later’));
$this->_redirect(‘*/*/’);
return;
}
} else {
$this->_redirect(‘*/*/’);
}
}
protected function _getCaptchaString($request, $formId)
{
$captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
return $captchaParams[$formId];
}
}
Now the captcha module is ready to test..
To apply captcha in contact us form go to your admin panel setting system->configuration->customer configuration->Captcha
Enable captcha if it disable you can see “Contact us Form” in multiselect form area select it and save configuration. see your captcha on front-end.