PDA

View Full Version : [HOW TO] Add additional fields


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

<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
03-12-2010, 04:35 PM
Nice Guide, Thanks.

nader
03-13-2010, 03:33 AM
what about CMS additional fields?

Dody
03-13-2010, 08:57 AM
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
04-17-2010, 12:25 AM
great can this be done for individual forum categories? For example not in lobby forum but in forum a b and z etc.

Dody
04-29-2010, 10:24 PM
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
05-04-2010, 02:18 AM
thanks alot for your good work

bnimbhal
06-07-2010, 03:49 PM
very tough to understand. :confused:

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

Dody
06-22-2010, 12:16 PM
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
07-20-2010, 01:35 AM
Can you show how to add the new value to a separate field in the thread table?

thanks in advance

libabom
07-28-2010, 10:46 AM
what about CMS additional fields?

vbbforfree
09-03-2010, 09:59 AM
how to add field before username

Dody
09-04-2010, 11:55 AM
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
09-04-2010, 09:25 PM
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
09-06-2010, 11:01 AM
which version it will be?

I believe it will be in 4.1.x

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

Thanks

Alec W
11-03-2010, 06:49 PM
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
11-19-2010, 02:44 PM
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
01-31-2011, 12:21 AM
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-06-2011, 10:54 PM
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
07-24-2011, 03:41 PM
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
08-02-2011, 08:22 PM
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
09-18-2011, 08:46 PM
works with the 4.1.5 version, the search mode is enabled?
thanks

cagatayh
01-28-2012, 06:44 PM
It doesnt work for me. What i want is literally:

https://vborg.vbsupport.ru/attachment.php?attachmentid=136147&stc=1&d=1327779094

https://vborg.vbsupport.ru/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
02-07-2012, 09:22 PM
It would be nice to see more of a step by step guide... I have no idea where to put all this code...

Abhik
02-13-2012, 10:20 AM
How to save this data in database instead of post?
Thanks

Jo_RHU
10-07-2014, 03:43 PM
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.