Back to vBulletin 3 Articles

Creating a New Thread using vBulletins API
by megamoose 06 Dec 2009
Rating: (1 vote - 4.00 average)

I really hope this will help some of you, as I spent ages searching the Internet tring to find something to do this, and ended up having to write my own.

This basically does exactly what the title says. It creates a new thread in a forum on your vbulletin forum.

It works for me in the latest version of vBulletin, but I'm not sure how far back it will work. But you should have your vBulletin up to date anyway.

If you notice any bugs/problems or anything I have done wrong, please correct me.

PHP Code:
<?php
/*======================================================================*\
|| #################################################################### ||
|| # Create a new thread                                              # ||
|| # ---------------------------------------------------------------- # ||
|| # Hopefully this will help some of you people who are needing to   # ||
|| # create a new thread externally using vBulletins API. Good luck!  # ||
|| #################################################################### ||
\*======================================================================*/

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE & ~8192);

// Include vBulletin runtime files
require_once('./global.php');
require_once(
'./includes/functions_databuild.php');

// Create a new datamanager for posting
$threaddm =& datamanager_init('Thread_FirstPost'$vbulletinERRTYPE_ARRAY'threadpost');

// Set some variable and information
$forumid 43;                                                                  // The id of the forum we are posting to
$userid 125500;                                                                 // The user id of the person posting
$title addslashes($_POST["title"]);                                           // The title of the thread
$pagetext addslashes($_POST["content"]);                                      // The content of the thread
$allowsmilie '1';                                                             // Are we allowing smilies in our post
$visible '1';                                                                 // If the post visible (ie, moderated or not)

// Parse, retrieve and process the information we need to post
$foruminfo fetch_foruminfo($forumid);
$threadinfo = array();
$user htmlspecialchars_unifetch_userinfo($userid) );

$threaddm->set_info('forum'$foruminfo);
$threaddm->set_info('thread'$threadinfo);
$threaddm->setr('forumid'$forumid);
$threaddm->setr('userid'$userid);
$threaddm->setr('pagetext'$pagetext);
$threaddm->setr('title'$title);
$threaddm->set('allowsmilie'$allowsmilie);
$threaddm->set('visible'$visible);

// Lets see what happens if we save the page
$threaddm->pre_save();
if(
count($threaddm->errors) < 1) {
    
// Basically if the page will save without errors then let do it for real this time
    
$threadid $threaddm->save();
    unset(
$threaddm);
} else {
    
// There was errors in the practice run, so lets display them
    
var_dump ($threaddm->errors);
}

?>
Thanks to ragtek for the revised code!

vblts.ru supports vBulletin®, 2022-2024