Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles

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
  #22  
Old 07-06-2011, 10:54 PM
radiofranky radiofranky is offline
 
Join Date: Jun 2011
Posts: 149
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the tutorial. Can this be expand to create an additional "prefix" field?

1st prefix ==> [laptop] my thread title - free shipping <= additional prefix

Thanks
Reply With Quote
  #23  
Old 07-24-2011, 03:41 PM
Raman Raman is offline
 
Join Date: Jun 2005
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Looked at the thread and I must say its been very informative. Thanks for writing this up.

For my need I have added a custom field called "customID" into thread and post table and my objective is to write this field to thread and post table for new threads.

My understanding is this that when we add new thread, logic writes one record in thread table and also in post table.

Any insight regarding that would be greatly appreciated
Reply With Quote
  #24  
Old 08-02-2011, 08:22 PM
radiofranky radiofranky is offline
 
Join Date: Jun 2011
Posts: 149
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi,
Could you expand a little bit on this article by adding storing those variable to DB and retrieve them for different template?

thanks
Reply With Quote
  #25  
Old 09-18-2011, 08:46 PM
paolpepas paolpepas is offline
 
Join Date: Dec 2008
Posts: 17
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

works with the 4.1.5 version, the search mode is enabled?
thanks
Reply With Quote
  #26  
Old 01-28-2012, 06:44 PM
cagatayh cagatayh is offline
 
Join Date: Aug 2006
Posts: 70
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It doesnt work for me. What i want is literally:





So i dont know really what i gotta do. In postbit before $post[message] as picture above code is:

<table>
<tr>
<td>
<img src="$picture" />
</td>

<td></td>
...
</tr>
</table>



I dont want to add it to the extra database table so it must come with $custom_message; but when it comes here how can i seperate picture url, how can i write table code above..

Thanks for your reply.
Attached Images
File Type: png vb.png (48.6 KB, 0 views)
File Type: png vbb.png (30.0 KB, 0 views)
Reply With Quote
  #27  
Old 02-07-2012, 09:22 PM
xertox xertox is offline
 
Join Date: Apr 2011
Location: Westlock AB
Posts: 18
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It would be nice to see more of a step by step guide... I have no idea where to put all this code...
Reply With Quote
  #28  
Old 02-13-2012, 10:20 AM
Abhik's Avatar
Abhik Abhik is offline
 
Join Date: Jun 2006
Location: Kolkata, India
Posts: 144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How to save this data in database instead of post?
Thanks
Reply With Quote
  #29  
Old 10-07-2014, 03:43 PM
Jo_RHU Jo_RHU is offline
 
Join Date: Jan 2010
Posts: 32
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Works great on 3.8.5, thanks so much! So simple yet I've been looking for something like this for years and had trouble with the forms plugins I could find.
Reply With Quote
Reply

Thread Tools

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 02:35 PM.


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.06998 seconds
  • Memory Usage 2,308KB
  • Queries Executed 26 (?)
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
  • (4)bbcode_php
  • (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
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (8)postbit
  • (2)postbit_attachment
  • (9)postbit_onlinestatus
  • (9)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_postinfo_query
  • fetch_postinfo
  • 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
  • postbit_attachment
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete