miniBB®Keeping it simple, but not stupid |
Home
Forums
Demo
Gallery of Arts
Showcase
Features
Requirements
Manual
Download Compiler Paid Extensions Paid Support License Contacts |
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:
Everything listed above could be achieved with non-desructive methods of miniBB. Adding New Field to Users Table in DatabaseFirst 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 FileAfter 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 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( 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 TemplateIf 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 --> 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: 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 PageNow let's study where user's custom field's value could appear:
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 PageListing only language variable, miniBB will display every new custom field AFTER "Total Number of Replies" field in user's 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: Parsing Profile's Value with a Special miniBB FunctionAs 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 */ 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: 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 */ 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: 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 MandatoryOnly 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 */ 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 --> 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. 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 SolutionYou 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: In the process of creating a site that was entirely work friendly we decided on using miniBB to power our forum (essentially the whole site). We created our own template and then made some modifications to the framework to streamline out what we were trying to achieve. Kevin |
miniBB.com © 2001-2024. All rights reserved. miniBB® is a registered trademark. @ Contact Us |