PDA

View Full Version : [HOW TO] Add additional fields


Dody
10 Mar 2010, 23:48
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:

<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:


<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:

$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:


$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:


$value = "".$value."";


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:


<input type="text" name="additionalFieldURL" />


to newthread template as described above

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


<?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 .= "Source URL: : ".$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 :)

MARCO1
12 Mar 2010, 18:35
Nice Guide, Thanks.

nader
13 Mar 2010, 05:33
what about CMS additional fields?

Dody
13 Mar 2010, 10:57
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.

weallgovern
17 Apr 2010, 02:25
great can this be done for individual forum categories? For example not in lobby forum but in forum a b and z etc.

Dody
30 Apr 2010, 00:24
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


<vb:if condition="$foruminfo[forumid] == x">
HTML fields
</vb:if>


Similary in the plugin code, you can add:


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

as7apcool
04 May 2010, 04:18
thanks alot for your good work

bnimbhal
07 Jun 2010, 17:49
very tough to understand. :confused:

wolfe
08 Jun 2010, 20:32
the bbcode parsing is not working properly i use [CODE] tags and it all shows in one line.

Dody
22 Jun 2010, 14:16
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.

neverstop
20 Jul 2010, 03:35
Can you show how to add the new value to a separate field in the thread table?

thanks in advance

libabom
28 Jul 2010, 12:46
what about CMS additional fields?

vbbforfree
03 Sep 2010, 11:59
how to add field before username

Dody
04 Sep 2010, 13:55
what about CMS additional fields?

I would advice you to wait until custom type, which is going to be released soon.

--------------- Added 1283604974 at 1283604974 ---------------

how to add field before username

It depends on what you are trying to achieve.

nader
04 Sep 2010, 23:25
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?

Dody
06 Sep 2010, 13:01
which version it will be?

I believe it will be in 4.1.x

reddyink
12 Sep 2010, 21:21
I need how-to guide for Extra Post Fields similar to extra thread fields written in this article.

Thanks

Alec W
03 Nov 2010, 20:49
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.

dcuellar
19 Nov 2010, 16:44
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:
<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 1290186736 at 1290186736 ---------------

Here is the product I have edited.

<?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 .= "Source URL: : ".$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?:

$custom_message .= "Source URL: : ".$value;
$vbulletin->GPC['message'] .= "".$custom_message;


And here is my Forum Block:
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'] == 0 )
{
$format = sprintf("On %s%s %s %s",date("j",$event['dateline_from'])+1, date("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.

matkus
31 Jan 2011, 02:21
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.

radiofranky
07 Jul 2011, 00:54
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

Raman
24 Jul 2011, 17:41
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 :)

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

thanks

paolpepas
18 Sep 2011, 22:46
works with the 4.1.5 version, the search mode is enabled?
thanks

cagatayh
28 Jan 2012, 19:44
It doesnt work for me. What i want is literally:

http://www.vbulletin.org/forum/attachment.php?attachmentid=136147&stc=1&d=1327779094

http://www.vbulletin.org/forum/attachment.php?attachmentid=136149&stc=1&d=1327779636

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.

xertox
07 Feb 2012, 22:22
It would be nice to see more of a step by step guide... I have no idea where to put all this code...

Abhik
13 Feb 2012, 11:20
How to save this data in database instead of post?
Thanks

Jo_RHU
07 Oct 2014, 17:43
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.