Friday, January 21, 2011

How to add extra fields in joomla registration


Let’s come to my blog post title “How to add extra fields in joomla registration and profile”. I was searching for solution about this but most common solution was put extra fields as joomla user meta tag. So let me show you how you can add extra fields in joomla registration and profile changing core files. I have tried to change less as much as possible.
Note: all file paths are windows style!

LIST OF FILES NEED TO EDIT/TOUCH

libraries\joomla\database\table\user.php it’s the class file tor user table/user object
Let we need to put two extra fields like phone and website, let’s these fields as optional. So we need to initialize two var as string.
find the follow lines and then need to put before that any php programmer will get easily but I am trying to write for all!
1 /**
2 * @param database A database connector object
3 */
4 function __construct( &$db )
now put the follow lines before above lines or near line 116


1 /**
2 * some extra fields like name
3 *
4 * @var string
5 */
6 var $phone          = null;
7 var $website        = null;


Again,


Open,
libraries\joomla\libraries\joomla\user\user.php
After line 124, add the following code



1 /**
2  * some extra fields like name
3  *
4  * @var string
5  */
6 var $phone          = null;
7 var $website        = null;

Yes we are 50% done already.
Now use your phpmyadmin to add alter your user table. Please don’t ask me how to work with phpmyadmin


1 ALTER TABLE jos_users ADD phone varchar(255) DEFAULT '' AFTER password;
2 ALTER TABLE jos_users ADD website varchar(255) DEFAULT '' AFTER phone;



Please take care about your table prefix. my table prefix. If you table prefix is something else like mysite then your user table will be like mysite_users and the sql code will be like this

1 ALTER TABLE mysite_users ADD phone varchar(255) DEFAULT '' AFTER password;
2 ALTER TABLE mysite_users ADD website varchar(255) DEFAULT '' AFTER phone;


Now go to administrator\components\com_users\views\user\tmpl
open file form.php and put the following lines after line 132 … just add as new row in table

        
Seems joomla doesn’t allow output overirde for admin component ! If that was possible then we didn’t need to change core file but put this view file in template folder. I will show you how to do that in front end uesr component. But before that Let me show you how it will look after the above changes done properly.
Now we need to do some changes for front end. Please go to components\com_user\views\register\tmpl
and now open file default.php for edit
After line 73 , after the password verify row … add these lines
Let me show you how this will l;ook in registration page if every thing is done properly.
I told you , we can do the same changes in view for front end component without changing core files. Copy that default.php file from components\com_user\views\register\tmpl and now go to your current template folder. Let’s it’s default rukhmilkway … Check there is a folder name html in it. if your custom template doesn’t have any html folder then create one. now go to the html folder and create a folder named “com_user” (if there is no folder with this name as some custom template use output override and use same method for styling) and create a folder in com_user folder as named “register” and paste the default.php file in it. Actually you need to copy all the php files from components\com_user\views\register\tmpl to templates\{your custom template name here}\html\com_user\register
Isn’t this fun ?
Oh we have left one thing still. If we want to give user to edit their profile from front end ? We need to edit one view file again. But this this time we will not change core file in view but at first copy the view from file front end component to template folder … follow the way I shown just now.
So according to default template go to templates\rhuk_milkyway\html\com_user\user (create folder “user” in com_user same way) and copy all the php files(two files default.php and form.php) from components\com_user\views\user\tmpl …. I think it’s now easy …
Open file form.php and edit/put new lines after 70(it’s just a end of a if-end condition)




Let me show another screenshot how the edit details from front end looks
that’s all. Let me know if you have any problem.