How to Include a PHP or HTML File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • feldon23
    Senior Member
    • Nov 2001
    • 11291
    • 3.7.x

    How to Include a PHP or HTML File

    Many people are familiar with the phpinclude_start and phpinclude_end templates of vBulletin 3. You could use these to include PHP in 2 or 3 places in vBulletin. It was not useful for extending control panels, manipulating users, threads, posts, etc.

    vBulletin 3.5 introduced Plugins which allow you to introduce PHP code into vBulletin in over 500 key areas or hooks. This allows you to extend vBulletin in dramatic ways with relatively simple code. More information on writing plugins and products is available at vBulletin.org.

    If you have a PHP or HTML file that you want to include in your vBulletin forum, create a plugin that references that file. Then add a variable to the template of your choice where that file's contents should appear.


    Including an HTML file:

    1. Create a Plugin for global_start with this code:
    Code:
    $includedhtml = implode('', file('[COLOR=Magenta]path/to/this/file/[/COLOR][COLOR=Red]myfile.html[/COLOR]'));
    replacing the path and filename with the correct path and filename of the HTML file you want to include. The contents of myfile.html will be copied to the variable $includedhtml.

    2. Place $includedhtml in one of your templates, such as header, navbar, FORUMHOME, depending upon where you want the contents of your HTML file to appear.

    Note: The Plugin system must be enabled in vBulletin Options -> Plugin System for plugins to work. It is disabled by default.


    Including a PHP file:

    1. Create a Plug-in for global_start with these contents:

    Code:
    ob_start();
       include('[COLOR=Magenta]path/to/this/file/[/COLOR][COLOR=Red]myfile.php[/COLOR]');
       $includedphp = ob_get_contents();
    ob_end_clean();
    replacing the path and filename with the correct path and filename of the PHP file you want to include. The code in myfile.php will execute and any output generated by this script will be stored in $includedphp.

    2. Place $includedphp in one of your templates, such as header, navbar, FORUMHOME, depending upon where you want the contents of your PHP file to appear.

    Note: The Plugin system must be enabled in vBulletin Options -> Plugin System for plugins to work. It is disabled by default.

    WARNING: Plugins that contain invalid or malicious code may cause your forum to stop functioning or even lead to data loss. Using Plugins is not supported and you'll be asked to disable them in the event that you request tech support. If a Plugin has made your forum inaccessible, please disable plugins. Troubleshooting errant plugins and products is handled at our sister site, vBulletin.org.


    What are ob_start(), ob_get_contents(), ob_end_clean(), etc? What is Output Buffering?
    Most PHP files that you might wish to include in your forum contain echo or other output statements in your PHP file, it will break vBulletin because it is still in the process of initializing when it loads your PHP file. All echo and other output commands must be output buffered using ob_start, ob_clean, etc. commands. The output of your PHP script will be buffered for later use and inserted into a variable. All other statements in the PHP script will execute normally.

    A word about variables.
    It is very important that any variables initialized in your PHP script do not overlap built-in vBulletin variables or you will get unpredictable results. It may be advisable to create a PHP script just for inclusion in your forum rather than including a larger script used by another part of your website.

    Which hook should I use?
    The hook I've used above (global_start) makes your HTML or PHP file available in almost every template on your vBulletin forum. You may wish to include a PHP file or HTML file only on certain pages or parts of your forum. You'll need to select the correct hook where your code should be loaded. To determine which hook you should use, turn on Debug and then make this change to the appropriate functions php file.

    Note: If you wish to include() multiple PHP files, make sure you use ob_clean() before each include() to reset the buffer.

    Adding and Editing Plugins (vBulletin User Manual) -- What the vBulletin User Manual has to say about Plugins.

Related Topics

Collapse

Working...