miniBB ® 

miniBB

®
Consume less - live more!
 
miniBB Home Home miniBB Community Forum and Bulletin BoardForums miniBB Forums Demo - Full Mode with Add-onsDemo The Gallery of miniBB Arts and Design LayoutsGallery of Arts miniBB Forums Worldwide ShowcaseShowcase miniBB Forum Software FeaturesFeatures miniBB Forum Script RequirementsRequirements Installation and Maintenance manual for your miniBB ForumManual
Forum Program and Plug-ins - Download for Free!Download Auto-CompilerCompiler Paid Extensions and Add-ons for your miniBB ForumPaid Extensions Paid Support for CustomersPaid Support miniBB Commercial License and Attribution Link RemovalLicense Contact UsContacts

Extending miniBB's User Profile with the New Field(s)

By Paul Puzyrev, author of miniBB

Beginning from the release version 2.1, miniBB is not only designed to support unlimited new fields in user's profile, but also to handle very custom values, all without destructing the core scripts. The process of adding such values is described below. It could be easy for you if you are a professional coder or at least not afraid to copy and paste pieces of code. However if you have no programming skills at all, I would recommend to stop reading at this point and hire a person, who could handle this job for you.

So in this article I will explain:

  • how to add a new custom field for member's Profile;
  • how to display this field's value on the Profile page;
  • how to display this field's value in the messages list near username;
  • how to parse this field using a special function, so the value displayed publically differs from the value stored in database;
  • how to use the same parsing function for displaying different values on the profile page and in topic messages;
  • how to change order of fields displayed on the Profile page;
  • how to make this field mandatory.

Everything listed above could be achieved with non-desructive methods of miniBB.

Adding New Field to Users Table in Database

First of all let yourself think, what exactly fields need to be created, and which SQL type you need to define for them. In most cases, each field either could have an INTEGER type (when there is stored a numerical value, and this might be useful for such form elements like dropdown menus); either could have a VARCHAR or TEXT type (this might be useful for form textareas and text input fields). In my example, I will create a field for Birthday and will have a DATETIME type, because Birthday always has a month, a year and a day.

Adding new field of any type, first create it in miniBB users table. For example, enter your mySQL interface (phpMyAdmin or whatever) and execute the following command (substituting minibbtable_users for the user's table name you possibly have changed):

alter table minibbtable_users add column user_custombd date not null default '0000-00-00';

Note: it is important that new profile field name begins with user_custom prefix. miniBB is designed to work with the custom fields only named that way.

Specifying New Field in miniBB's Options File

After the field is created, define it in setup_options.php, extending variable array called $dbUserSheme. This array starts with the key, the key defines array of 3 values - first is numerical index for that field in the table; second is the title of the table's field; third is the name of the input element in the profile form (templates/user_dataform.html). For defining index, download our automated "Define fields" script, copy it to miniBB folder and point an URL to it like:

http://here_goes_your_domain/forum/determine_fields.php

You should see then the name of your new field at the end of the list, and notice the index at the very bottom. Since I've used default installation of miniBB, this field has an index "21". But it could vary of course. After that, I defined the field in $dbUserSheme:

$dbUserSheme=array(
'username'=>array(1, 'username', 'login'),
'user_password'=>array(3, 'user_password', 'passwd'),
'user_email'=>array(4, 'user_email', 'email'),
'user_icq'=>array(5, 'user_icq', 'icq'),
'user_website'=>array(6, 'user_website', 'website'),
'user_occ'=>array(7, 'user_occ', 'occupation'),
'user_from'=>array(8, 'user_from', 'from'),
'user_interest'=>array(9, 'user_interest', 'interest'),
'user_viewemail'=>array(10, 'user_viewemail', 'user_viewemail'),
'user_sorttopics'=>array(11, 'user_sorttopics', 'user_sorttopics'),
'language'=>array(14, 'language', 'language'),
'num_topics'=>array(16, 'num_topics', ''),
'num_posts'=>array(17, 'num_posts', ''),
'user_custom1'=>array(18, 'user_custom1', 'user_custom1'),
'user_custom2'=>array(19, 'user_custom2', 'user_custom2'),
'user_custom3'=>array(20, 'user_custom3', 'user_custom3'),
'user_custombd'=>array(21, 'user_custombd', 'birthday')
);

Note: there is no comma after last element in the array, but there is a comma after all other elements.

Most likely you need to define field's key the same as field's name, but for the HTML form we can specify another value. In my case, I named this form element "birthday". That way nobody will know how the profile field in your database is called (yes, it's some security issue, but it's not major - even if you give the same name to the field's title in database and to the element in the profile form, there should be nothing insecure).

Adding New Field in User's Profile Form Template

If we would have a simple string field, there is nothing else left as to add this field in the form itself. Edit the file /templates/user_dataform.html and paste the following HTML snippet where you prefer (I prefer to add this after "Interests & Hobbies" in default version):

<!-- birthday field -->
<tr><td class="caption4" style="text-align:right;vertical-align:top">Birthday</td>
<td class="caption5">
<input type="text" name="birthday" maxlength="150" size="20" value="{$birthday}"
class="textForm" style="width:200px;">
</td></tr>
<!-- /birthday field -->

If you log-in to forums and open your Profile section at this stage, or will try to sign up a new user, you will see that this field is already available. Enter it in a valid date format ( YYYY-MM-DD, where YYYY is 4-digits year, MM is 2-digits month and DD is 2-digits day), save your preferences and check the result:

New Text Field in miniBB Profile Form

But - do you think all users are experienced enough to enter this value properly? If valid date is not entered, mySQL will automatically reset the value to '0000-00-00'. I will explain a bit later, how to handle the valid date. In general, this is only my custom case. For the non-mandatory text-type fields, there would be nothing else to implement, and you don't need to read below. If you would like to implement a mandatory field however there are needed additional functions to handle it. Study below.

Displaying New Field's Value on the Profile Page

Now let's study where user's custom field's value could appear:

  • User's public profile (appears when you click on a "Member" title in the thread, or other places)
  • User's message in the thread

For displaying Birthday in public profile, you need to add the following variable in language pack:

$l_usrInfo[15] = 'Birthday';

Notice the index of that variable - if this is the first custom field added by you (not counting default miniBB custom fields like "user_custom1"), the index will be 15. In any other case, it will be [counting_number_of_the_new_field] + 14.

Changing Sequence of Displaying Profile Values on the Profile Page

Listing only language variable, miniBB will display every new custom field AFTER "Total Number of Replies" field in user's profile:

Displaying New Field in miniBB Profile

For changing this sequence, define $customProfileList array in setup_options.php the following way:

$customProfileList=array('username', 'user_regdate', 'user_email', 'user_icq', 'user_website', 'user_occ', 'user_from', 'user_interest', 'user_custombd', 'num_topics', 'num_posts');

In other words - specify all field names of the user profile table which you want to be listed, and they will be listed in the order you set:

Displaying New Field in miniBB Profile

Parsing Profile's Value with a Special miniBB Function

As already shown on the screenshots above, it would be nice to have a human-readable/editable field instead of standart SQL-format date. That means, instead of '1977-12-28' we will display '28 Dec 1977'. For that case, we can program special PHP function, which will parse this field and return "normal" value. For each custom field you add, this function must be called parseUserInfo_[FIELD_NAME], where FIELD_NAME substitutes for the field name in database, user_custombd in our case. For example, paste this function in bb_plugins.php:

/* user_custombd parsing function */
function parseUserInfo_user_custombd($val){
if($val=='0000-00-00') $newDate=''; else {
$months=explode(':', $GLOBALS['l_months']);
$exData=explode('-', $val);
if(substr($exData[1],0,1)=='0') $mm = (integer) substr($exData[1], 1, 1) + 0; else $mm=(integer)$exData[1]+0;
if(substr($exData[2],0,1)=='0') $exData[2]=(integer)substr($exData[2],1,1)+0;
$newDate=$exData[2].' '.$months[$mm-1].' '.$exData[0];
}
return $newDate;
}
/* --user_custombd parsing function */

Check your profile now - this function must return an empty value (empty values will not be listed in the profile automatically by miniBB) if the date is equal to '0000-00-00'; it must return a human-readable date if something is entered for this field.

Other case is to display user's birthday in the messages thread. For example near his nickname. miniBB has built-in routine called "user info in posts". You need to enable this setting in setup_options.php:

$userInfoInPosts = array('user_custombd');

or specify as many profile field names as you like, separating them by commas (this is the way how simple arrays in PHP are set). Then, edit /templates/main_posts_cell.html file and paste this variable under {$viewReg}:

{$userInfo_user_custombd[$poster_id]}

Hopefully, you understand the logics of how variable must be named and how it looks in your case.

After that, in each user's message, you should see his birthday date:

Displaying New Field in miniBB Thread

It seems a bit boring 'cause there is no break after member's title, and... which purpose is to simply display user's birthday in a post? Let's improve our function listed above and make it displaying China Horoscope sign. I will program little additional function for that:

/* user_custombd parsing functions */

function determineSign($year){

$horoscopes=array(
1976=>'Dragon',
1977=>'Snake',
1978=>'Horse',
1979=>'Sheep',
1980=>'Monkey',
1981=>'Rooster',
1982=>'Dog',
1983=>'Pig',
1984=>'Rat',
1985=>'Ox',
1986=>'Tiger',
1987=>'Rabbit'
);

if(isset($horoscopes[$year])) return $horoscopes[$year];
elseif($year>1987) { $ytmp=$year; while(!isset($horoscopes[$ytmp])) $ytmp=$ytmp-12; }
elseif($year<1976) { $ytmp=$year; while(!isset($horoscopes[$ytmp])) $ytmp=$ytmp+12; }
return $horoscopes[$ytmp];

}

function parseUserInfo_user_custombd($val){

if($val=='0000-00-00') $newDate=''; else {

if($GLOBALS['action']=='vthread'){
$year=substr($val,0,4);
$newDate=determineSign($year);
}

elseif($GLOBALS['action'] == 'userinfo'){
$months=explode(':', $GLOBALS['l_months']);
$exData=explode('-', $val);
if(substr($exData[1],0,1)=='0') $mm=(integer)substr($exData[1],1,1)+0; else $mm=(integer)$exData[1]+0;
if(substr($exData[2],0,1)=='0') $exData[2]=(integer)substr($exData[2],1,1)+0;
$newDate=$exData[2].' '.$months[$mm-1].' '.$exData[0];
}

}

if($GLOBALS['action']=='vthread' and $newDate!='') $newDate='<br />'.$newDate.'<br />';
return $newDate;
}

/* --user_custombd parsing function */

Paste the code above in bb_plugins.php, replacing the previous older version - and you will see there a China Horoscope sign is displayed in messages thread, but only clear Birthday date (not the Horoscope) in the user's profile:

Displaying New Field in miniBB Thread

This example shows how using the same function, you can catch the $GLOBALS['action'] parameter and parse user's profile value differently for the topic page and profile page.

Having a bit of PHP skills and programming additional functions by the principles listed above, you may reach unbelievable results in customizing your board. You may provide even such functions which other forums software even doesn't allow. If you are still in doubt how to do this and that correctly, don't forget we are providing programming of additional functions and adding custom fields for very acceptable fees.

Validating New Form Field upon Entering and/or Making It Mandatory

Only one question is left here: what if user enters his birthday date incorrectly? What if he doesn't know, which format to use? What if in general, I need to add not the date field, not the string field, but just some list of options, which user may choose from? I am suggesting to use built-in miniBB function called makeValuedDropDown and create 3 dropdown lists, where user will choose the year, month and day of his Birthday. Paste in bb_plugins_user.php the following code:

/* making dropdown fields for Birthday */

if($action=='registernew'){
$bd_year='0000';
$bd_month='00';
$bd_day='00';
}

elseif ($action=='prefs'){
if(isset( $userData[ $dbUserSheme['user_custombd'][0] ] )) {
list($bd_year, $bd_month, $bd_day) = explode('-', $userData[$dbUserSheme['user_custombd'][0]]);
}
else {
$bd_year='0000';
$bd_month='00';
$bd_day='00';
}
}

elseif($action=='register' or $action=='editprefs'){

if(isset($_POST['bd_year']) and isset($_POST['bd_month']) and isset($_POST['bd_day'])) {
$bd_year = (integer)$_POST['bd_year'] + 0;
$bd_month = (integer)$_POST['bd_month'] + 0;
$bd_day = (integer)$_POST['bd_day'] + 0;

if(!checkdate($bd_month, $bd_day, $bd_year)) {
$bd_year='0000'; $bd_month='00'; $bd_day='00';
$correct=10;
$l_userErrors[10]='Birthday date appears not to be valid!';
}

else{
if($bd_month<10) $bd_month='0'.$bd_month;
if($bd_day<10) $bd_day='0'.$bd_day;
}

$_POST['birthday'] = $bd_year.'-'.$bd_month.'-'.$bd_day;
$user_custombd = $_POST['birthday'];

}

else {
$_POST['birthday']='';
}

}

$years=array(); $years['0000']=' ';
for($i=date('Y'); $i>=date('Y')-100; $i--) $years[$i]=$i;
$yearDropDown = makeValuedDropDown($years, 'bd_year');
$months=array(); $months['00']=' ';
$mn2=explode(':', $l_months); for($i=0; $i<12; $i++) $months[$i+1]=$mn2[$i];
$monthsDropDown = makeValuedDropDown($months, 'bd_month');
$days=array(); $days['00']=' '; for($i=1; $i<32; $i++) $days[$i]=$i;
$daysDropDown = makeValuedDropDown($days, 'bd_day');

/* --making dropdown fields for Birthday */

Don't forget, that you will also need to paste new variables in templates/user_dataform.html, and the final piece of HTML snippet will look like:

<!-- birthday field -->
<tr><td class="caption4" style="text-align:right;vertical-align:top">Birthday</td>
<td class="caption5">
{$daysDropDown} {$monthsDropDown} {$yearDropDown}
</td></tr>
<!-- /birthday field -->

I.e., instead of text field, you will have dropdown fields for year, month and day separately, and they will be saved also when you edit the profile.

New DropDown Field in miniBB Profile Form

This script also checks the date for validity - if user, for example, enters February 31 (which is not existing date), script will reset the date to the empty values and won't allow to proceed the form. Pay attention to the $correct variable's value and $l_userErrors error definition. Registration and profile scripts won't proceed if $correct is not equal to 0. All possible default error values of it are proceeding in bb_func_checkusr.php file. Here, we could catch the new values for the new fields. If you would like to make a non-mandatory field, just skip setting of these variables.

Also pay attention that $_POST['birthday'] (the index corresponds to the 2nd value in $dbUserSheme['user_custombd']) and $user_custombd (variable's name corresponds to the 1st value of $dbUserSheme['user_custombd']) need to be defined separately, if the exact variable is not provided in the form.

Implementing a mandatory field is also possible with JavaScript solution provided at our forums. With this solution all checking will be done at the client side; server side function is much easier in implementation.

Additional Examples of This Solution

You may check another example of implementing custom field via profile's plugin parsing engine, analyzing the code for Signatures solution. Pay attention of how the value of the specified custom field could be parsed on the fly and which array variable may be used for this. Example of a Gender solution provides the code for working out the field which value is not mandatory and could be kept in a simple dropdown list.

Ring Bells! The End of Lecture. Thank You for Your Attention.

Forum owners about miniBB:
It's really easy to set up, the other boards you have to configure whole loads of crap.
as written by...chroIVIium
miniBB ® miniBB.com © 2001-2024. All rights reserved.
miniBB® is a registered trademark.
@ Contact Us 


  ⇑