3

I need to show default magento captcha in contact-us page.

  • Check link: stackoverflow.com/a/33743012,this will help you. – akgola Oct 11 '17 at 9:33
  • I mistakenly added the solution for magento 2 . – Dinesh Yadav Oct 11 '17 at 10:23
  • I complete all the steps but it doesn´t work by me - Magentoshop 1.9 – Adela Dec 29 '18 at 19:32
  • Please dont add your question as answers – Jai Dec 30 '18 at 2:34
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – Rama Chandran M Dec 30 '18 at 5:24
15

Created a new module for this.

Step 1:

app/code/local/Contact/Captcha/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Contact_Captcha>
        <version>0.1.0</version>
    </Contact_Captcha>
</modules>
<default>
    <captcha>
        <frontend>
            <areas>
                <contact_page_captcha>
                    <label>Contact Page</label>
                </contact_page_captcha>
            </areas>
        </frontend>
    </captcha>
</default>
 <global>
    <models>
        <contact_captcha>
            <class>Contact_Captcha_Model</class>
        </contact_captcha>
    </models>
    <events>
        <controller_action_predispatch_contacts_index_post>
            <observers>
                <captcha>
                    <type>model</type>
                    <class>contact_captcha/observer</class>
                    <method>checkContactPage</method>
                </captcha>
            </observers>
        </controller_action_predispatch_contacts_index_post>
    </events>
</global>
</config>

Step 2:

app/etc/modules/Contact_Captcha.xml

<?xml version="1.0"?>
<config>
<modules>
    <Contact_Captcha>
        <active>true</active>
        <codePool>local</codePool>
    </Contact_Captcha>
</modules>
</config>

Step 3:

app/code/local/Contact/Captcha/Model/Observer.php

<?php

class Contact_Captcha_Model_Observer extends Mage_Captcha_Model_Observer
{
public function checkContactPage($observer)
{
    $formId = 'contact_page_captcha';
    $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
    if ($captchaModel->isRequired()) {
        $controller = $observer->getControllerAction();
        if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
            Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
            $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
            $controller->getResponse()->setRedirect(Mage::getUrl('*/*/'));
        }
    }
    return $this;
}
}

Step 4:

Create a local.xml in theme path.

app/design/frontend/theme/default/layout/local.xml

<?xml version="1.0"?>
<layout version="0.1.0">
 <contacts_index_index translate="label">
    <reference name="contactForm">
        <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_page_captcha</formId></action>
                <action method="setImgWidth"><width>230</width></action>
                <action method="setImgHeight"><width>50</width></action>
            </block>
        </block>
    </reference>
</contacts_index_index>
</layout>

Step 5:

app/design/frontend/theme/default/template/contacts/form.phtml

Add below code:

 <?php echo $this->getChildHtml('form.additional.info'); ?>

Step 6:

Admin Settings:

Dashboard - > Customers - > Customer Configuration - > CAPTCHA - > Forms - > Select Contact Page

Step 7:

Clear cache. Refresh contact us page.

  • Your answer is better than mine. – Mohit Kumar Arora Oct 11 '17 at 9:55
  • 4
    For what it's worth, in my Magento 1.9, I had to add the following code in app/code/local/Contact/Captcha/etc/config.xml within the tag <default>: <customer> <captcha> <always_for> <contact_page_captcha>1</contact_page_captcha> </always_for> </captcha> </customer> – medina Mar 30 '18 at 3:37
  • How can I apply the same code for a CMS Page? I'm loading the Contact form inside a CMS Page. I have: www.domain.com/contact , and here I load the contact form using: {{block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"}} . Thanks. – Pascut Jun 27 '18 at 19:37
  • For me it didn't work at first: Warning: include(): Failed opening '/includes/src/Contact_Captcha_Model_Observer.php' I had to manually copy the Observer.php file over to the includes/src directory for it to work. Any idea why? – Víctor Aug 9 '18 at 15:11
  • 1
    @Víctor because your shop ist compiled, you should disable / re-enable this when installing a module – simonthesorcerer Dec 10 '18 at 8:18
3

You need to either create a custom module or install an extension for this.

Below is the code for implementation as a custom module:

app/etc/modules/Mohit_Contactcaptcha.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mohit_Contactcaptcha>
            <active>true</active>
            <codePool>local</codePool>
        </Mohit_Contactcaptcha>
    </modules>
</config>

app/code/local/Mohit/Contactcaptcha/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mohit_Contactcaptcha>
            <version>0.1.0</version>
        </Mohit_Contactcaptcha>
    </modules>
    <frontend>
        <layout>
            <updates>
                <contactcaptcha>
                    <file>contactcaptcha.xml</file>
                </contactcaptcha>
            </updates>
        </layout>
    </frontend>
    <global>
        <models>
            <contactcaptcha>
                <class>Mohit_Contactcaptcha_Model</class>
            </contactcaptcha>
        </models>
        <events>
            <controller_action_predispatch_contacts_index_post>
                <observers>
                    <contactcaptcha>
                        <class>contactcaptcha/observer</class>
                        <method>checkContacts</method>
                    </contactcaptcha>
                </observers>
            </controller_action_predispatch_contacts_index_post>
        </events>
    </global>
    <default>
        <captcha>
            <frontend>
                <areas>
                    <contacts>
                        <label>Contacts Page</label>
                    </contacts>
                </areas>
            </frontend>
        </captcha>
        <customer>
            <captcha>
                <always_for>
                    <contacts>1</contacts>
                </always_for>
            </captcha>
        </customer>
    </default>
</config>

app/code/local/Mohit/Contactcaptcha/Model/Observer.php

<?php
class Mohit_Contactcaptcha_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');
                $controller->getResponse()->setRedirect($url);
            }
        }
        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];
    }
}

app/design/frontend/default/default/layout/contactcaptcha.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <contacts_index_index>
        <reference name="contactForm">
            <action method="setTemplate"><template>contactcaptcha/contacts/form.phtml</template></action>
            <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>contacts</formId></action>
                    <action method="setImgWidth"><width>230</width></action>
                    <action method="setImgHeight"><width>50</width></action>
                </block>
            </block>
        </reference>
    </contacts_index_index>
</layout>

app/design/frontend/default/default/template/contactcaptcha/contacts/form.phtml

<?php
/**
 * Magento
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
<div class="page-title">
    <h1><?php echo Mage::helper('contacts')->__('Contact Us') ?></h1>
</div>
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">
    <div class="fieldset">
        <h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
                    <div class="input-box">
                        <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
                    <div class="input-box">
                        <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
                    </div>
                </div>
            </li>
            <li>
                <label for="telephone"><?php echo Mage::helper('contacts')->__('Telephone') ?></label>
                <div class="input-box">
                    <input name="telephone" id="telephone" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text" type="text" />
                </div>
            </li>
            <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>
                </div>
            </li>
            <?php echo $this->getChildHtml('form.additional.info'); ?>
        </ul>
    </div>
    <div class="buttons-set">
        <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
    </div>
</form>
<script type="text/javascript">
//<![CDATA[
    var contactForm = new VarienForm('contactForm', true);
//]]>
</script>

After creating the module, login to admin panel and navigate to Store > Configuration > Customers > Customers Configuration > Captcha and select Contacts Page from given options

Please let me know if you find any problem.

  • Thnks for the reply. .I have already find the solution and posted the answer. – Rita Jose Oct 11 '17 at 9:47
  • Great to know that you have found the solution. Also thanks for sharing your findings. – Mohit Kumar Arora Oct 11 '17 at 9:51
  • How can I apply the same code for a CMS Page? I'm loading the Contact form inside a CMS Page. I have: www.domain.com/contact , and here I load the contact form using: {{block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"}} . Thanks. – Pascut Jun 27 '18 at 19:37
0

how to fix Contact-us page captcha not showing in magento 1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.