Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Closed Thread
 
Thread Tools
[How-To] vBulletin API Basics: Creating Custom Pages & Misc.
akanevsky
Join Date: Apr 2005
Posts: 3,972

 

Show Printable Version Email this Page Subscription
akanevsky akanevsky is offline 10-09-2005, 10:00 PM

Ever wanted to create your own vBulletin-powered page but didn't know how? With this [How-To], you can.

NOTE: This is a vBulletin 3.5 version of Gary King's Manual, updated and expanded.

NOTE TO CODERS: THIS TUTORIAL CONTAINS MOST UP TO DATE INFORMATION IN THE WHOLE THREAD. IF ANY FURTHER POSTS BY 3RD PARTIES (INCLUDING OTHER CODERS) CONTAIN ANY INFORMATION THAT COMES INTO A CONFLICT WITH ANYTHING SAID IN THIS TUTORIAL, IT CAN BE CONSIDERED FALSE, UNLESS I VERIFY IT.

BASICS

Creating a custom vBulletin-Powered Script
  1. Create a new file, whatever you want to call it (let's say test.php).
    Open up the newly created file and fill in the following code:

    PHP Code:
    <?php

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

    // ##################### DEFINE IMPORTANT CONSTANTS #######################
    // change the line below to the actual filename without ".php" extention.
    // the reason for using actual filename without extention as a value of this constant is to ensure uniqueness of the value throughout every PHP file of any given vBulletin installation.

    define('THIS_SCRIPT''test'); 

    // #################### PRE-CACHE TEMPLATES AND DATA ######################
    // get special phrase groups
    $phrasegroups = array();

    // get special data templates from the datastore
    $specialtemplates = array();

    // pre-cache templates used by all actions
    $globaltemplates = array(
            
    // change the lines below to the list of actual templates used in the script
            
    'test_mytesttemplate1',
            
    'test_mytesttemplate22',
    );

    // pre-cache templates used by specific actions
    $actiontemplates = array();

    // ########################## REQUIRE BACK-END ############################
    require_once('./global.php');

    // #################### HARD CODE JAVASCRIPT PATHS ########################
    $headinclude str_replace('clientscript'$vbulletin->options['bburl'] . '/clientscript'$headinclude);

    // ########################################################################
    // ######################### START MAIN SCRIPT ############################
    // ########################################################################

    $navbits = array(); 
    // change the line below to contain whatever you want to show in the navbar (title of your custom page)
    $navbits[$parent] = 'Test Page';

    $navbits construct_navbits($navbits);
    eval(
    '$navbar = "' fetch_template('navbar') . '";');

    // change the line below to contain the name of the actual main output template used in your script
    eval('print_output("' fetch_template('test_mytesttemplate1') . '");');

    ?>
  2. Read through the code above and change what's commented with "// change this...".
  3. $navbits, just as any other array, can contain an unlimited of items. Note that in $navbits, array key serves an URL (in this case, pre-defined variable $parent contains an URL to the current script itself), and array value is what will actually be displayed as a link in your HTML output.

    Another example:
    PHP Code:
    $navbits['somepage.php'] = 'Sometitle';
    $navbits['somepage2.php'] = 'Sometitle2';

    $navbits construct_navbits($navbits);
    eval(
    '$navbar = "' fetch_template('navbar') . '";'); 
    The above is going to result in "Sometitle" serving as a link to "somepage.php", followed by "Sometitle2" serving as a link to "somepage2.php".
  4. Create template test_mytesttemplate1. Paste in the following content:

    HTML Code:
    $stylevar[htmldoctype]
    <html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
    <head>
    $headinclude
    <title>$vboptions[bbtitle]</title>
    </head>
    <body>
    
    $header
    $navbar
    
    <table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" 
    
    align="center">
    <tr>
    <td class="tcat">My Custom Page Title</td>
    </tr>
    <tr>
    <td class="alt1">Other Bluff...</td>
    </tr>
    </table>
    
    $footer
    </body>
    </html>

You are done!

Note that template names are prefixed with test_. Prefixing is a good practive, as it makes it easier to manage the mass later on. This applies not only to templates, but also to custom phrases and options.

On to the next section:

Adding Custom Online Locations

If you want Who's Online to reflect your new custom page when someone is browsing it (rather than Unknown Location), do the following steps:
  1. Open AdminCP.
  2. Plugin System -> Add New Plugin
  3. Fill in the following:

    Code:
    Product: vBulletin
    Hook Location: online_location_process
    Title: My Custom Location (Part1)
    Plugin PHP Code: if ($filename == 'test.php')
    {
            $userinfo['activity'] = 'test';
    }
    Plugin is Active: Yes
  4. Click Save.
  5. Plugin System -> Add New Plugin
  6. Fill in the following:

    Code:
    Product: vBulletin
    Hook Location: online_location_unknown
    Title: My Custom Location (Part2)
    Plugin PHP Code: if ($userinfo['activity'] == 'test')
    {
            $userinfo['action'] = 'Viewing Test Page'; // you might wanna use a $vbphrase here...
            $userinfo['where'] = '<a href="./test.php?' . $vbulletin->session->vars['sessionurl'] . '">This is My Test Page</a>'; // you might wanna use a $vbphrase here...
            $handled = true;
    }
    Plugin is Active: Yes
  7. In each of the above two, make sure to change "test" values to your own.
  8. Note that $userinfo['where'] is optional. It can be controlled with a usergroup permission "can view detailed location".

You are done!

Using vBulletin-powered scripts outside vBulletin Directory

REPLACE

PHP Code:
require_once('./global.php'); 
WITH

PHP Code:
chdir('/home/site/public_html/forums');
require_once(
'./global.php'); 
Where "/home/site/public_html/forums" must be replaced with an actual system path to your forums.
Also, make sure you add the following code in the beginning of any relative links:

Code:
$vbulletin->options['homeurl']
Creating "Subpages"

If you want to create "subpages" within your custom page, simply wrap blocks of code with the following structure:

PHP Code:
if ($_REQUEST['do'] == 'test'

    
// Block of code #1
}

if (
$_REQUEST['do'] == 'test2'

    
// Block of code #2

Limiting Access to the Script

Registered Members Only

UNDER

PHP Code:
require_once('./global.php'); 
ADD

PHP Code:
if (!$vbulletin->userinfo['userid'])
{
        
print_no_permission();

Certain Usergroups Only (In this example, 6 and 7)

PHP Code:
if (!is_member_of($vbulletin->userinfo6) AND !is_member_of($vbulletin->userinfo7))
{
        
print_no_permission();

You can also use the limits in your vBulletin templates. Here are to examples that you can use (but you are not limited to using them, so utilize your imagination):

HTML Code:
<if condition="is_member_of($bbuserinfo, 6)">
If in group 6, show this...
<else />
...or else, show this.
</if>
HTML Code:
<if condition="!$bbuserinfo['userid']">
Shown to registered members only.
<else />
Shown to everyone else.
</if>
ADVANCED CODING
Do not read below unless you have a basic knowledge of PHP and a general idea of what is it that you are doing.

vBulletin Phrase Replacements

As you might now, vBulletin supports phrase replacement. In other words, if your phrase (in this example, "testphrase") contains the following text,

Code:
The user {1} has written {2} posts.
What's it for? Well, if you use the following PHP code, you get "The user testuser has written 10 posts" in $testvar:

PHP Code:
$testvar construct_phrase($vbphrase['testphrase'], "testuser""10"
You can use as many replacement as you want in the construct_phrase() function. 1st replacement corresponds to {1}, 2nd - to {2}, Nth - to {N}... You can also use these replacements in templates, with a slightly different syntax:

HTML Code:
<phrase 1="testuser" 2="10">{$vbphrase['testphrase']}</phrase>
Using Variables in Templates

To use variables in templates, follow these rules:
  • "Regular" Variables ($somevar, $somevar2, etc).
    Use anywhere within template text.
  • "Array-Type" Variables ($somevar['someval'], etc).
    Avoid using single quotes, otherwise you get a parse error. In other words, use $somevar[someval] instead of $somevar['someval'].
  • "Object-Type" Variables ($vbulletin->GPC['somevar']).
    Do use single quotes, but wrap such variables into figure brackets. One valid example is {$vbulletin->GPC['somevar']}.
    If you attempt using $vbulletin->GPC['somevar'] or $vbulletin->GPC[somevar], you will get an error.

Warning: As a security measure, avoid using arrays $_POST[], $_GET[], $_REQUEST etc., in your templates - even though you may if you really need to.

Using Conditionals in Templates

vBulletin 3.0.0 and up features templates conditionals. Example:

HTML Code:
<if condition="$somevar">
        htmlcode
<else />
        other html code
</if>
In conditionals (the part highlighted in blue), and ONLY in conditionals, you should use the regular PHP variable-naming syntax (disregarding what's said in "Using Variables in Templates" part of this tutorial). Just put in whatever you would normally put into the if() clause.

Notably, vBulletin does not feature an "else if" clause within its templates system. Therefore if you need more than one conditional, you can nest them. Example:

HTML Code:
<if condition="$condition1">
        htmlcode
<else />
        <if condition="$condition2">
                more html code
        </if>
</if>
You can nest any number of conditional clauses, as long as the code is valid.

Valid vBulletin URL Formation
  • Every URL to a vBulletin page within the same site must contains a sessionurl.
    Note: You do not need to add an ampersand (&) after the sessionurl.

    In PHP Code, an example of a valid URL would be:

    PHP Code:
    $url "mytestpage.php?" $vbulletin->session->vars['sessionurl'] . "moreGETstring"
    In templates, you can use $session[sessionurl] instead. E.g.:

    HTML Code:
    mytest.php?$session[sessionurl]moreGETstring
  • If you pass more than one parameter using $_GET, use &amp; instead of just &.
    This way you will preserve valid XHTML code.

Creating Custom Standard Error Messages

Read very carefully. To show standard error messages, you must:
  1. Create a phrase in Front-End Error Messages, prefixed with "error_". The prefix is mandatory.
  2. Use the following PHP code in your manual:

    PHP Code:
    eval(standard_error(fetch_error('error_myphrase'))); 
    Be sure to change "error_myphrase" to your actual phrase (again, the real phrase must be prefixed with "error_").

Creating Redirects

To create a redirect you must:
  1. Make sure there is no output before the redirect, otherwise it might not work correctly.
  2. Define redirect URL using the following PHP code:

    PHP Code:
    $vbulletin->url "mytestpage.php?" $vbulletin->session->vars['sessionurl'] ."somevar=someval"
  3. Do not forget to replace "mytestpage.php" with an actual filename.
  4. Make sure to leave $vbulletin->session->vars['sessionurl'] part intact, otherwise the page might not work correctly.
  5. Make sure to replace "somevar=someval" with any variables and their respective values that you want to pass to the next script via $_GET array.

Once you are done with that:
  1. Create a phrase in "Front-End Redirect Messages" group. Must be prefixed with "redirect_".
  2. Add the following code where you want to activate the redirect:

    PHP Code:
    eval(print_standard_redirect('redirect_myphrase'truetrue)); 
  3. Be sure to replace "redirect_myphrase" with an actual phrase name.
  4. Be sure that you end up having a "redirect_" prefix.

Done!

>> END OF MANUAL
  #2  
Old 01-15-2006, 01:22 PM
Brad Brad is offline
 
Join Date: Nov 2001
Posts: 4,765
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Please direct any questions to this forum:

https://vborg.vbsupport.ru/forumdisplay.php?f=111
Closed Thread


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 01:56 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.03631 seconds
  • Memory Usage 2,264KB
  • Queries Executed 15 (?)
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
  • (4)bbcode_code
  • (7)bbcode_html
  • (13)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (2)post_thanks_box
  • (4)post_thanks_box_bit
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (2)post_thanks_postbit_info
  • (1)postbit
  • (2)postbit_onlinestatus
  • (2)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
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete