View Full Version : Insert custom thread field into the database
josh_krz
01-30-2014, 07:01 PM
Hi there,
I have a good understanding of the vB templating system and PHP knowledge but I am having trouble working out how I could add a custom field on my thread pages to a new column in the "thread" table.
I know how I could add info to the table using PHP prepared statements but apparently it is better to use vBulletins own database classes however I can't find information on it anywhere!
I also don't know how I would get the generated thread ID to use as a reference when inserting this info or what my custom field would be called, I can't just use $POST['field']. These variables seem to be a mystery.
Advanced vBulletin documentation seems to be pretty scarce am I supposed to guess these variables and class names :S.
Thank you.
I don't know of any documentation, but I threw together a bare bones example of adding a field to the thread table, and I've attached the product xml (you'd only want to import this to a test forum). Two things this example doesn't do: create the database field and add the input field to the newthread template. I called my new field "test" and made it a string, so to use this example product you'd need to add a field to your thread table called "test", and add an input control to the form in the newthread template, and set name="test". Alternatively you could edit the code and change the field name and type to something else.
If you have any questions please ask, but if you don't already have the vb source code set up so you can search it, you should do that. If you search for the hook names you can see a little about what's going on at the point where each plugin in executed.
josh_krz
01-30-2014, 09:30 PM
Thank you kh99, so all I need to do is create the database column and add the field within the template for this to work?
Looking at that XML these things seems pretty straight forward, if there were some definite way to know what classes and functions to use :(
How do people learn how to code plugins, there must be some cache of resources somewhere outlining these things?
Thanks once again!
ozzy47
01-30-2014, 09:36 PM
No there is no tutorial, basically you have to know, php, html, css, and have a understanding of how vBulletin works. Then take it and put it all together.
PHP, basic guide: PHP 5 Tutorial (http://www.w3schools.com/php/)
HTML basic guide: HTML Tutorial - (HTML5 Compliant) (http://www.w3schools.com/html/)
CSS basic guide: CSS Tutorial (http://www.w3schools.com/css/)
vBulletin basic mod guide: Creating a Product (http://www.vbulletin.com/docs/html/creating_a_product)
Thank you kh99, so all I need to do is create the database column and add the field within the template for this to work?
Right. Also, it should automatically be read in so in most places where you have a $threaddata array, your field value should be there. So for example if you wanted to display the value on the showthread page, you shouldn't need to do anything extra to read it from the database.
BTW, another thing this example doesn't handle is editing the thread.
Looking at that XML these things seems pretty straight forward, if there were some definite way to know what classes and functions to use :(
How do people learn how to code plugins, there must be some cache of resources somewhere outlining these things?
Yeah, I don't know how most people learned, but I just started reading the code. For example if I wanted to add a field to a thread, I'd see that when I'm creating a new thread it goes to newthread.php, so I'd look at that to see what was going on (yeah, it's easy for me to say but I know it takes a lot of time).
josh_krz
01-31-2014, 05:15 PM
No there is no tutorial, basically you have to know, php, html, css, and have a understanding of how vBulletin works. Then take it and put it all together.
Thanks for your help but im already pretty comprehensive in PHP 5, HTML 5 and CSS 3. It's just the vBulletin part I thought would have been documented. That link is helpful though!
Right. Also, it should automatically be read in so in most places where you have a $threaddata array, your field value should be there. So for example if you wanted to display the value on the showthread page, you shouldn't need to do anything extra to read it from the database.
BTW, another thing this example doesn't handle is editing the thread.
I see so I would need to add a plugin that also reads the current info from the database and adds it in when editing?
Yeah, I don't know how most people learned, but I just started reading the code. For example if I wanted to add a field to a thread, I'd see that when I'm creating a new thread it goes to newthread.php, so I'd look at that to see what was going on (yeah, it's easy for me to say but I know it takes a lot of time).
I was initially going to do this but I don't have a test vBulletin site to do this on just my production one. I was worried about messing my site up.
I will take a look at some of the internal code and see what I can make of it. Thank you very much!
grey_goose
04-04-2016, 06:53 PM
This seems fairly straightforward but I guess I'm messing something up.
1. I've added 'expenditure' to the 'post' table.
2. I'd like to be able to to add/update this field when replies to a thread are made.
newpost_process
if ($type == 'thread')
{
$dataman->setr('expenditure', $post['expenditure']);
}
newreply_post_start
$vbulletin->input->clean_gpc('p', 'expenditure', TYPE_STR);
$newpost['expenditure'] = $vbulletin->GPC['expenditure'];
newreply_start
$this->validfields['expenditure'] = array(TYPE_STR, REQ_NO);
newreply template
<input type="text" name="expenditure" />
When I look at my post table, there's no values being added to expenditures. I also tried:
$expenditure = $vbulletin->input->clean_gpc('p', "expenditure", TYPE_STR);
$vbulletin->post['expenditure'] += $expenditure;
Where am I going wrong?
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.