Add captcha to contact form in magento

If you need to add captcha in contacts page, you need to add this. So lets start how to add captcha in contacts page.
NameSpace : MyPackage and Module: MyModule

Module configuration

<!-- app/etc/modules/MyPackage_MyModule.xml -->

Create config file for this module

<!-- app/code/local/MyPackage/MyModule/etc/config.xml -->
<?xml version="1.0"?>
                        <label>Contacts Page</label>

Create a observer

// app/code/local/MyPackage/MyModule/Model/Observer.php
class MyPackage_MyModule_Model_Observer
    public function checkContacts($observer){
        $formId = 'contacts';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        if ($captchaModel->isRequired()) {
            $controller = $observer->getControllerAction();
            $word = $this->_getCaptchaString($controller->getRequest(), $formId);
            if (!$captchaModel->isCorrect($word)) {
                Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
                $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
                $url =  Mage::getUrl('contacts');
        return $this;
     * Get Captcha String
     * @param Varien_Object $request
     * @param string $formId
     * @return string
    protected function _getCaptchaString($request, $formId)
        $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
        return $captchaParams[$formId];

Create a local.xml to your active theme inside layout folder

<?xml version="1.0"?>
<layout version="0.1.0">
        <reference name="contactForm">
            <action method="setTemplate"><template>mymodule/contacts/form.phtml</template></action>
            <block type="core/text_list" name="">
                <block type="captcha/captcha" name="captcha">
                    <reference name="head">
                        <action method="addJs"><file>mage/captcha.js</file></action>
                    <action method="setFormId"><formId>contacts</formId></action>
                    <action method="setImgWidth"><width>230</width></action>
                    <action method="setImgHeight"><width>50</width></action>

Now copy “contacts/form.phtml” to “mymodule/contacts/form.phtml”, add

 <?php echo $this->getChildHtml(''); ?>

to your requirement. example…

<li class="wide">
                <label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment') ?></label>
                <div class="input-box">
                    <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
            <?php echo $this->getChildHtml(''); ?>

Clear magento cache.

Now Go to System -> Configuration -> Customer Configuration -> Captcha. Select Contact Page and Save.

Happy Coding!

19 thoughts on “Add captcha to contact form in magento

  1. It’s working but only for default contact page, I want to use it for my custom url: contact-us but it is only working for contacts(which is default).
    please help !!

  2. Hello,
    I can see it show in the backend (Contacts Page) but not in the frontend under the comments box. It’s not loading any html. What could I be doing wrong? The js is also included in the head. I know the right form is being loaded because when I enter some test text it shows on the page.

  3. This extension is not working in Contact us page or any custom form in Magento1.9.0.1 after enabling in Admin->Customer Configuration->Captcha. According to this extension details, it has been created a custom module that has config.xml, Observer.php under app/code/local/My_Extension/My_Module and also created a local.xml under layout of theme and also copy/paste the Contacts folder under templates/my_module and in form.phtml file, getChildHtml(‘’); ?> it has been added. But it is unfortunately not working in Magento1.9.0.1. I can’t understand where I am wrong.If any one knows about it, please reply back. Thank you.

      1. On line 19 of your config.xml you have “mymodule/observer” but is that supposed to be “mymodule/model/observer” like you have defined in Observer.php?

        On my system the captcha doesn’t display but when I hit Submit it shows that it’s required.

      2. Hi,
        Don’t worry about config.xml. It’s working of version 1.7.x, 1.8.x, 1.9.x . For your case which version you try? Don’t forget to clear cache.

  4. It works pretty well for magento 1.8. Thank you very much for your solution.
    But if I try to rename module and package names, I don’t get a selection in the magento backend to choose the contact form. Maybe a reference to namespaces would be helpful, but it works in general.

  5. Where to copy contacts/form.pthml is it in theme files app/design/frontend/default/themename/mymodule/contacts/form.phtml?

  6. Thanks It is working.
    Just one question if i want to set template layout to be 2 column with left bar then what has to be done?

  7. nice post, i have one suggestion instead of rendering CAPTCHA using default magnento setting we can create our settings and use it, in some cases user only want it on contact us page at that time it will be useful.

    need to change : “<block type="captcha/captcha" type of block with our custom block and extends it from "Mage_Captcha_Block_Captcha_Zend"

    overwrite function : _toHtml

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.