Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
[HOW TO] Add additional fields
Dody
Join Date: Jul 2004
Posts: 7

 

Show Printable Version Email this Page Subscription
Dody Dody is offline 03-09-2010, 10:00 PM

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
Reply With Quote
  #2  
Old 03-12-2010, 04:35 PM
MARCO1's Avatar
MARCO1 MARCO1 is offline
 
Join Date: Jun 2008
Posts: 872
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Nice Guide, Thanks.
Reply With Quote
  #3  
Old 03-13-2010, 03:33 AM
nader nader is offline
 
Join Date: Feb 2004
Posts: 63
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

what about CMS additional fields?
Reply With Quote
  #4  
Old 03-13-2010, 08:57 AM
Dody Dody is offline
 
Join Date: Jul 2004
Posts: 7
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't think that CMS has enough hooks to do the above, but as Edwin Brown blogged, you can create your own content type which includes the number of fields that suites your need.
Reply With Quote
  #5  
Old 04-17-2010, 12:25 AM
weallgovern weallgovern is offline
 
Join Date: Apr 2010
Posts: 7
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

great can this be done for individual forum categories? For example not in lobby forum but in forum a b and z etc.
Reply With Quote
  #6  
Old 04-29-2010, 10:24 PM
Dody Dody is offline
 
Join Date: Jul 2004
Posts: 7
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by weallgovern View Post
great can this be done for individual forum categories? For example not in lobby forum but in forum a b and z etc.
Yes it is possible. In the newthread template you can use

PHP Code:
<vb:if condition="$foruminfo[forumid] == x">
HTML fields
</vb:if> 
Similary in the plugin code, you can add:

PHP Code:
    if($foruminfo[forumid] == 9){
    
//catching the fields vars
     

Reply With Quote
  #7  
Old 05-04-2010, 02:18 AM
as7apcool as7apcool is offline
 
Join Date: Feb 2009
Posts: 194
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks alot for your good work
Reply With Quote
  #8  
Old 06-07-2010, 03:49 PM
bnimbhal bnimbhal is offline
 
Join Date: Jul 2009
Posts: 29
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

very tough to understand.
Reply With Quote
  #9  
Old 06-08-2010, 06:32 PM
wolfe wolfe is offline
 
Join Date: Jan 2002
Posts: 900
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

the bbcode parsing is not working properly i use [CODE] tags and it all shows in one line.
Reply With Quote
  #10  
Old 06-22-2010, 12:16 PM
Dody Dody is offline
 
Join Date: Jul 2004
Posts: 7
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by wolfe View Post
the bbcode parsing is not working properly i use [CODE] tags and it all shows in one line.
I am not sure if that could be a bug in the code tag.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:49 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05215 seconds
  • Memory Usage 2,323KB
  • Queries Executed 24 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (3)bbcode_html
  • (6)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete