Don't wait… it will take me some time…

How to Add a Custom Field in Contact Us Form for Joomla 1.7

I needed to add the Phone field in the Contact Us form in the website I was building. I didn’t wanna use any Joomla contact form plugin though since I think it would be an overkill since I just needed to add a single custom field. Took me a while to figure this out so thought I’d share to the world a summarized version of my research hoping this will save some people time.

DISCLAIMER: I’m not a PHP nor a Joomla expert so if you see any discrepancy, do let me know. Also, suggestions, corrections and reactions are most welcome.

Step-by-Step process on adding a custom field

Add the Custom Field in the Model

Add the Custom Field in the View

Add the Custom Field in the Email Sent to Contact Form’s Recipient

 

Add the Custom Field in the Model

  1. Go to /<joomla_home>/components/com_contact/models/forms and edit contact.xml
  2. Add the field you want (in my case, I added contact_telephone)
  • NOTE: I still have to figure out what are the available built in values for the fields type, description, label, filter and where to modify them. I will update this post when I’ve figured that out. The way I’ve figured what values to use is just to look at the other xml and PHP files.
  • The name and id could be anything

modify model1 How to Add a Custom Field in Contact Us Form for Joomla 1.7

 

Add the Custom Field in the View

  1. Go to /<joomla_home>/components/com_contact/views/contact/tmpl and edit default_form.php
  2. Add the custom field you just created. Make sure to use the same name that you indicated in the contact.xml from the previous step

modify view How to Add a Custom Field in Contact Us Form for Joomla 1.7

 

Modify Controller: Add the Custom Field in the Email Sent to Contact Form’s Recipient

  1. Go to /<joomla_home>/components/com_contact/controllers and open contact.php
  2. Modify the _sendEmail($data, $contact) function
    1. Add the custom field in the $data variable (see line139)
    2. Add the custom field in the $body variable which is the container for the email content (see line 145)

modify controller How to Add a Custom Field in Contact Us Form for Joomla 1.7

 

11 Responses to “How to Add a Custom Field in Contact Us Form for Joomla 1.7”

  1. joseph says:

    php code… halimaw ka hehe…miss ko yan

  2. João Sales says:

    Thank you!

  3. BionicNellyWiz says:

    It is not really the best way to do this because it’s affecting the core of Joomla and the core of Joomla gets regularly overwritten with updates. I have to admit it works really nicely of the method you mention about and it doesn’t in the override method – think I’ve got something missing and can’t put my finger on it…so not sure how to solve it.

  4. BionicNellyWiz says:

    Ok, I’ve researched for the best way to implement the above script without touching the Joomla! core files.

    Before you follow the step by step process above.

    1) Put the default_form.php into your template folder (e.g. /templates/NAMEOFYOURTEMPLATE/html/com_contact/contact) If you don’t have “html” directory, do create it along with the sub-directories of com_contact and inside that directory the “contact” directory and it should look like that to the above directory structure.

    2) Now copy the joomla’s core file of contact.php that is in //components/com_contact/controllers and do the editing as shown above in your copy of the contact.php and put it in the same directory of /templates/NAMEOFYOURTEMPLATE/html/com_contact/contact. Now you can edit your copy of the contact.php as shown above in the Step by Step process.

    3) Next go to //components/com_contact/models/forms and copy contact.xml into your folder (/templates/NAMEOFYOURTEMPLATE/html/com_contact/contact) and do the editing as shown in the Step by Step above.

    2) Now open your copy of the default_form.php file that is now in /NAMEOFYOURTEMPLATE/html/com_contact/contact/default_form.php with your favourite notebook/text editor and put in at the very top before the <?php…:

    Next, add the following, just insert it before the Joomla license comments of:

    $this->form->reset( true ); // to reset the form xml loaded by the view
    $this->form->loadFile( dirname(__FILE__) . DS . “contact.xml”); // to load in our own version of contact.xml

    Save your default_form.php file and refresh your website. It should work! It did for me.

    It is important to never touch the joomla! core because it is automatically updated to the latest joomla version.

    I have to say that I’m not an php expert but just by researching on the internet was the best way of finding out the solution. I’ve certainly learnt alot and wanted to share it with you and hopefully helps you too. Good luck!

  5. BionicNellyWiz says:

    I forgot to say at the very top before the <?php…, insert:

  6. BionicNellyWiz says:

    Ok here it goes again, the php coding I tried to insert keeps disappearing so am putting in quotes around it to hopefully from stopping it to strip:

    “”

  7. BionicNellyWiz says:

    include ( dirname(__FILE__) . DS . “contact.php”);

  8. BionicNellyWiz says:

    Ok it didn’t work with the php tag being included.

    “”

  9. BionicNellyWiz says:

    You’ll have insert the left angle bracket followed by question mark and the word php before the include command and at the end of the contact.php”); put in question mark by right angle bracket.

    • Jett says:

      Thanks a lot for this additional info! I needed a quick fix on the site which won’t be modified for a long time so I figured I’ll just modify the core joomla file. You are right though, this will be overwritten if you perform an upgrade. If you’ll be doing an upgrade often, using a 3rd-party plugin will be a better idea.

  10. I was searching for a way to add custom fields to the default Joomla contact form, and I fount it here. When you change the core files of Joomla it works, but if you create the template override it does not. It is all in the code “include (dirname etc”. In this way it does not include the file. Is there another way or code that works?

Leave a Reply to João Sales

Powered by WordPress | Designed by Elegant Themes