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
  #12  
Old 07-20-2010, 01:35 AM
neverstop neverstop is offline
 
Join Date: Jan 2007
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Can you show how to add the new value to a separate field in the thread table?

thanks in advance
Reply With Quote
  #13  
Old 07-28-2010, 10:46 AM
libabom libabom is offline
 
Join Date: Dec 2005
Posts: 30
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

what about CMS additional fields?
Reply With Quote
  #14  
Old 09-03-2010, 09:59 AM
vbbforfree vbbforfree is offline
 
Join Date: Jan 2010
Posts: 24
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

how to add field before username
Reply With Quote
  #15  
Old 09-04-2010, 11:55 AM
Dody Dody is offline
 
Join Date: Jul 2004
Posts: 7
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by libabom View Post
what about CMS additional fields?
I would advice you to wait until custom type, which is going to be released soon.

--------------- Added [DATE]1283604974[/DATE] at [TIME]1283604974[/TIME] ---------------

Quote:
Originally Posted by vbbforfree View Post
how to add field before username
It depends on what you are trying to achieve.
Reply With Quote
  #16  
Old 09-04-2010, 09:25 PM
nader nader is offline
 
Join Date: Feb 2004
Posts: 63
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dody View Post
I would advice you to wait until custom type, which is going to be released soon.

It depends on what you are trying to achieve.
which version it will be?
Reply With Quote
  #17  
Old 09-06-2010, 11:01 AM
Dody Dody is offline
 
Join Date: Jul 2004
Posts: 7
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nader View Post
which version it will be?
I believe it will be in 4.1.x
Reply With Quote
  #18  
Old 09-12-2010, 07:21 PM
reddyink reddyink is offline
 
Join Date: Aug 2007
Posts: 236
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I need how-to guide for Extra Post Fields similar to extra thread fields written in this article.

Thanks
Reply With Quote
  #19  
Old 11-03-2010, 06:49 PM
Alec W Alec W is offline
 
Join Date: Dec 2009
Posts: 15
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How would you use $value = $vbulletin->input->clean_gpc('p', "additionField", TYPE_STR); to get both the value and text from a dropdown list?

Never mind. Figured it out.
Reply With Quote
  #20  
Old 11-19-2010, 02:44 PM
dcuellar dcuellar is offline
 
Join Date: Nov 2007
Posts: 104
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Glad I found this article. Too bad I have yet to figure out what I'm trying to do. Hopefully someone here can help.

I'm trying to add a field in the template "calendar_edit" for people to add an image url to calendar events. I got the field visible when creating an event by using the following:
Code:
			<div class="blockrow">
					<label for="imgfield" class="full">Enter IMG URL:</label>
					<input type="text" class="primary full textbox" id="imgfield" name="imgurl" value="{vb:raw img_url}" tabindex="1"/>
					<p class="singledescription">This field is important. It will place this image on our Forum Home page's "Upcoming Events" side block.</p>
			</div>
What I'm struggling with is the next step. So you are saying I have to create a plugin for the data to be saved in order to use it later? Because as it is, it is not saving the data I place in that field.

Eventually, I want to place this image in a sideblock for upcoming events but it's not saving the field entered data.

--------------- Added [DATE]1290186736[/DATE] at [TIME]1290186736[/TIME] ---------------

Here is the product I have edited.

PHP Code:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<product productid="imgurl" active="1">
 <title>Calendar Image URL</title> 
 <description>Adds an image URL field to calendar events.</description> 
 <version>0.0.1</version> 
  
 <dependencies> 
 </dependencies> 
 <codes> 
 </codes> 
 <templates> 
 </templates> 
 <stylevardfns> 
 </stylevardfns> 
 <stylevars> 
 </stylevars> 
 <plugins> 
  <plugin active="1" executionorder="5"> 
   <title>calendar image field</title> 
   <hookname>newthread_post_start</hookname> 
   <phpcode><![CDATA[ 
         $value = $vbulletin->input->clean_gpc('p', "imgurl", 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>
What do I use as the hookname? I don't think its the same as "newthread_post_start".

Does everything else look right? Do I need this for what I'm trying to accomplish?:

PHP Code:
         $custom_message .= "[customBBcode][b]Source URL: [/b] [/customBBcode]: ".$value;
         
$vbulletin->GPC['message'] .= "".$custom_message

And here is my Forum Block:
PHP Code:
ob_start(); 


//  %d 
$show_count 1

$query sprintf("SELECT * FROM ".TABLE_PREFIX."event WHERE visible = 1 AND (dateline_from > '%d' || (  dateline_from > '%d' AND dateline_to > '%d' )) ORDER BY dateline_from ASC LIMIT %d",TIMENOW,TIMENOW,TIMENOW,$show_count);   

$event_get vB::$db->query_read($query); 

$output_bits ''
while(
$event vB::$db->fetch_array($event_get)) { 

     if(
$event['dateline_to'] == 
     { 
         
$format sprintf("On %s%s %s %s",date("j",$event['dateline_from'])+1date("S",$event['dateline_from']), date("M",$event['dateline_from']), date("Y",$event['dateline_from'])); 
     } else { 
         
$format sprintf("From %s to %s",date('jS M Y',$event['dateline_from']),date('jS M Y',$event['dateline_to'])); 
     } 
      
     
$output_bits .= sprintf(
        <div class = "cms_widget_post_bit"><center><a href="calendar.php?do=getinfo&e=%d"><img src="{vb:raw imgurl}" width="200px" alt="Upcoming Event"/><br /><br /><h4 class="cms_widget_post_header" style="font-weight:bold;">%s</a></h4> 
            <p class="cms_widget_post_content">%s</p> <br />Who will win? <a href="vbookie.php">Bet on it!</a></center>
        </div> 
        '
,$event['eventid'],$event['title'],$format); 


$output $output_bits


ob_end_clean(); 
return 
$output
I placed {vb:raw imgurl} in the image source just to see if it worked. Nope, it didn't.
Reply With Quote
  #21  
Old 01-31-2011, 12:21 AM
matkus matkus is offline
 
Join Date: Jan 2004
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How do i save it as separate column in post or thread table instead of modifying post message? It could be much more useful i think.
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 09:14 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.05961 seconds
  • Memory Usage 2,359KB
  • 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
  • (1)bbcode_code
  • (3)bbcode_html
  • (7)bbcode_php
  • (4)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
  • (11)post_thanks_box
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)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