Overloading the Magento Contact Controller

Published by John on November 10, 2012 Under Magento

Recently, one of my clients who uses Magento decided to to offer a new promotion and as a result needed to setup a custom form.

Setting up multiple contact forms in is easy enough by copying template/contacts/form.phtml, updating it as needed, and then within the new CMS page, adding {{block type=”core/template” name=”contactForm” form_action=”/contacts/index/post” template=”contacts/new_form.phtml”}} to the page content.

From there, updating the email template and using if, else, and depends provides a passable solution, even when some of the fields are different for each form.

However, I soon realized that the default contact form setup has more than a few limitations, especially when working with multiple forms. For instance, if you wanted to have a custom redirect for each form, custom responses, or wanted to add additional tracking methods to the form, it ends up being easier to just modify the Mage_Contact controller.

Table of Contents

This how-to ended up being a bit long, length wise due to printing the contents of the various resource files. So, I have provided a table of contents for the various sections.

A good place to start, if you just want to jump right in, would be the Overview section!

The Importance of Not Modifying the Core Files

It is generally a good idea to avoid modifying a software’s core files, such as making an edit to a file in WordPress ‘s wp-includes folder or editing files in Magento’s Core Mage folder. This is primarily to ensure you can update the software later without loosing any changes you might have made, but also to make life easier on those working behind you, which seems to often end up being future-me.

Preserving Changes in Magento and Controllers

With most other Magento core files, over-riding them is as simple as copying the php file to your /app/code/local/Mage directory, while preserving the folder hierarchy. For instance, if you wanted to change how the invoice pdf was generated, you could copy some of the files under /app/code/core/Mage/Sales/Model/Order/Pdf/ into /app/code/local/Mage/Sales/Model/Order/Pdf/.

This is not the case with the file used to control the contact form(app/code/core/Mage/Contacts/controllers/IndexController.php,) as it is a controller and so it needs to be overloaded in a different manner.

Instead, you will need to create a custom package, or plugin, in-order to make your changes update-proof.

Make a Backup

As is the case with any sort of modification, make a backup of your Magento Directory and database before continuing!

Overview for Overriding the Mage_Contacts_IndexController Class

The basic process is thus:

  1. Add Your Module, which over-rides the Contact Controller, to your /app/code/local Directory
  2. Add an xml file to /app/etc/modules, activating your new custom module
  3. Clear Magento Cache(System->Cache Management-> Refresh All and Flush Magento Cache

Adding Your Module to the Local Folder

First, you will need to create the folders/files necessary for your module. You can download this example here: Magento Contact Extended

The KCR folder should be uploaded to the /app/code/local/ directory and has the following structure:

|-- ContactExtended
    |-- Helper
 	-- Data.php
    |-- controllers
    	-- IndexController.php
    |-- etc
 	-- config.xml

The files are as follows:


<?xml version="1.0"?>

                        <KCR_ContactExtended before="Mage_Contacts">KCR_ContactExtended</KCR_ContactExtended>





class KCR_ContactExtended_Helper_Data extends Mage_Core_Helper_Abstract



require_once 'Mage/Contacts/controllers/IndexController.php';
class KCR_ContactExtended_IndexController extends Mage_Contacts_IndexController

 public function postAction()
	echo 'GOOD!';



Note that the above will simply print “GOOD!” and die, breaking your contact form. You would want to modify ‘KCR/ContactExtended/controllers/IndexController.php’ as needed.

Activating Your Module

Now that you have your module added, you need to tell Magento about it. Upload KCR_ContactExtended.xml to your /app/etc/modules/ folder.

It should be as follows:


<?xml version="1.0"?>

Clear Your Cache and Verify Module is Found

Next, clear your cache in Magento, by logging into the admin backend and going to System->Cache Management

Select all and refresh, as well as going ahead and clicking on “Flush Magento Cache”

Next, goto System->Configuration->Advanced. You should see your new module listed under “Disable Modules Output” with the name “KCR_Contact_Extended” and it should be set to “Enable”

Verify it is Working

The file “/app/code/local/KCR/ContactExtended/controllers/IndexController.php” overloads the postAction function. When you submit a contact form, it should now print “Good!” and then php should die.

You would obviously want to change this and can override any of the functions in Mage/Contacts/controllers/IndexController.php in a similar manner. Notice that you can also use “parent::postAction();” if you wanted to still call the default ‘postAction’ function.

Getting Help

Hopefully this will help some folks out and please don’t hesitate to comment and I will try to reply as best I can.

The above process works correctly in Magento 1.6 and 1.7 for me. However, Magento is rather complicated, with the documentation and community support forums often leaving something to be desired. So, before attempting the above, you should have a somewhat established understanding of php.

Please note that my main goal was to change the redirect, add better support for multiple forms, and provide a bit better spam/user tracking. As such, being able to over-ride the postAction function was my main need.



  1. Kai on Dec 30, 2012

    Thank you very much!! Your post is the answer I need for setting up a custom magento contact form.

  2. Dwe on May 15, 2013

    Thanks! This worked perfect!

  3. john on May 16, 2013

    I am glad you found this post useful!

    I think it ends up being a good starting point for setting up multiple contact forms in Magento and if the forms have the same fields, you really don’t have to do too much more work to it. Otherwise, you can get pretty fancy with how the data is processed and the confirmation emails that are sent.

  4. Rinkal on Sep 29, 2013

    Thanks for this.

    I got your plugin work perfect. Thanks for that. Can you please let me know that once setting up 2 contact us forms, now i want different post URLs, how can i do that.?

    Thanks for all your help.

  5. john on Oct 11, 2013

    Do you mean setting up two different contact form pages, so people access them via different URLS? Or do you want them to both post to different Controllers. Both would be possible, although you could also modify this contact form controller to process different forms in a different way. Otherwise, if you just need to setup a new form, you can do that from the backend of Magneto.

  6. Santiago on Apr 22, 2015


    I was looking for this tutorial for a long time ago

    Thanks for this man!!

Add a Comment