Back to vBulletin 4 Articles

[HOW TO] Add additional fields
by Dody 11 Mar 2010
Rating: (2 votes - 5.00 average)

This guide will show you how to add additional fields to your threads. This feature has been requested many many times and can be very powerfull.

Imagine that you want to add additional fields to your threads, such that every time a user create a new thread has the opportunity to fill these fields. This requires HTML and PHP experience, so be prepared!

Add additional fields

The very first step is to the the HTML code of the additional fields into newthread template. This can be done by adding HTML code directly into the HTML code right after the Title field:

HTML Code:
<input type="text" class="primary full textbox" name="subject" id="subject" value="{vb:raw subject}" maxlength="{vb:raw vboptions.titlemaxchars}" tabindex="1" />
    &nbsp;<img id="display_posticon" src="{vb:raw selectedicon.src}" alt="{vb:raw selectedicon.alt}" />
   </div>
For instance you can add:

HTML Code:
<input type="text" name="additionalField" />
Or you can add a variable ({vb:raw additionalfields}) and render newthread template to add these fields automatically from a plugin (if you don't know how, go for adding the HTML code or do some homework )

Catch additional fields values

Now that you have add some fields to newthread template, you need to catch them before adding them to the database. This can simply be done by hooking newthread_post_start and catching the field value:

PHP Code:
$value $vbulletin->input->clean_gpc('p'"additionField"TYPE_STR); 
Where additionalField is HTML name attribute of the additional field we added above.

Add additional fields values to post/database

Now we have catched the field value ($value) and are ready to save it to the database or to the post.

Personally I prefer to add it to the post, because it will be searchable by default and can be done very easily (easier than adding it to an extra database table)

Once you cashed the field variable as described above, you can add it to the post by:

PHP Code:
$vbulletin->GPC['message'] = $vbulletin->GPC['message']."(padding goes here)".$value

Extra BBcodes

If you want to wrap $value with some extra HTML codes, then you need to create some custom BBcodes and wrap it around the value:

PHP Code:
$value "[customBBcode]".$value."[/customBBcode]"
This requires that you add the BBcodes manually before everything else.

Case study
Let's say we want to add an additonal field that will carry the source URL of an article.

First we add:

HTML Code:
<input type="text" name="additionalFieldURL" />
to newthread template as described above

Then create a plugin (something similar to the following).

PHP Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<product productid="additionalFields" active="1">
 <title>Additional Fields</title>
 <description>Addition Fields</description>
 <version>0.0.1</version>
 <url />
 <versioncheckurl />
 <dependencies>
 </dependencies>
 <codes>
 </codes>
 <templates>
 </templates>
 <stylevardfns>
 </stylevardfns>
 <stylevars>
 </stylevars>
 <plugins>
  <plugin active="1" executionorder="5">
   <title>additional fields</title>
   <hookname>newthread_post_start</hookname>
   <phpcode><![CDATA[
         $value = $vbulletin->input->clean_gpc('p', "additionlFieldURL", TYPE_STR);
         $custom_message .= "[customBBcode][b]Source URL: [/b] [/customBBcode]: ".$value;
         $vbulletin->GPC['message'] .= "".$custom_message;
 ]]></phpcode>
  </plugin>
 </plugins>
 <phrases>
 </phrases>
 <options>
 </options>
 <helptopics>
 </helptopics>
 <cronentries>
 </cronentries>
 <faqentries>
 </faqentries>
</product>
Or download it from the attachment below. Add it through Admincp -> Plugins & Products -> Manage products, and [Add/Import Product]

And you are done!

Note: the plugin is just to show you how this works and by no mean is supported or ready for production.

Got a question, suggestion or improvment? don't hesitate to let me know

vblts.ru supports vBulletin®, 2022-2024