Including Custom PHP in Templates.

Collapse
X
Collapse
  •  

  • Including Custom PHP in Templates.

    We've created a new package that is intended to facilitate the replacement of custom PHP modules with custom Display Template modules. It also demonstrates how to create custom API functions in general. You will find this package attached below.

    Steps to replace a PHP module Module

    Edit the api/main.php file

    1. Find the customWidgetHtml function
    2. Copy your php code from the module into the function.
    3. Change the code to generate a string value instead of echoing content and set that to the $myhtml parameter.

    If you have more than one PHP module you can copy the function, give it a new name, and copy/edit the code above for each module.

    1. Upload this package directory into core/packages

    Create a custom template

    1. Go to the style editor in the admincp and select add a new template for the site style you are using.
    2. Give it a name like 'customtemplatemodule'
    3. copy the following into the template changing the name to match the function name(s) in the api/main.php:
    Code:
    {vb:data result, localapi:main, customWidgetHtml}
    {vb:raw result.html}
    1. Do this for each PHP module you are replacing.

    Adding Your Code to a Page

    1. Go to sitebuilder.
    2. Edit the page that you want to display the module on.
    3. Add a "Display Template" Module to replace the PHP Module
    4. Set the template name to 'customtemplatemodule' or whatever you named your new template.
    5. Remove the old PHP module template
    6. Save the Page.

    Note you can return html and display it from the API in this way. It's the quickest and easiest way of porting from an existing PHP module to a Template module. But it isn't necesarily the best way. The API functions are intended to return data that can be inserted into the markup in the templates. And alternative example is provided in the sample class. You can uncomment the function if you want to try it out. The template to use this to provide the same result as the sample html return is:

    Code:
    {vb:data result, localapi:main, dataOnly}
    <p>{vb:var result.hello}</p><p></b>{vb:var result.hellobold}</b></p>

    As above save that as a custom template and create a Display Template Module that uses it.

    Attached Files
    Last edited by Wayne Luke; Tue 26 Apr '22, 3:38pm.

    • raywjohnson
      #18
      raywjohnson commented
      Editing a comment
      Yep. That should work.

      So this line determines the path, script, method, and return var

      {vb:data result, localapi:main, dataOnly}

      Breakdown

      vb:data result : result contains the returned data (associative array / json.object)

      localapi:main : the path and script: i.e. /core/packages/localapi/api/main.php

      dataOnly : the method to call

      So you can put the scripts all in the same package, or a new package for each, up to you.

      Example:

      {vb:data retval, package2:stuff, getData}

      /core/packages/package2/api/stuff.php : method getData : returns data to variable retval

    • eolesen
      #19
      eolesen commented
      Editing a comment
      Is there an easy way to reference within a localAPI script some standard vBulletin variables like the username, usergroup, user ID?

      I'm currently using:

      Code:
       global $vbulletin;  
         $userid = $vbulletin->userinfo[userid];
         $username = $vbulletin->userinfo[username];
      but shouldn't those already be stored as globals I can grab inside the localapi shell? Are there other secret variables we can pull short of having to invoke them as above in each function?

    • eolesen
      #20
      eolesen commented
      Editing a comment
      Originally posted by Trainsim Operations
      Can I safely have more than one PHP file being used i.e. main.php for common functions, jellydonut.php for something task specific, lasagna.php for something else task specific, and simply change out the php file reference in the custom tag e.g.

      {vb:data result, localapi:main, dataOnly}

      {vb:data result, localapi:jellydonut somethingSweet}

      {vb:data result, localapi:lasagna, itsHotAndMessy}

      OK... I figured out you CAN have multiple PHP files inside the localAPI folder, but the (one of many, apparently) undocumented step is that the class declaration in the top line of each additional PHP file in the API subfolder has to be modified to form a new API class.

      Continuing the example above, you can have files main.php, lasagna.php and jellydonut.php

      For lasagna.php:
      Code:
      class localapi_Api_[b]Lasagna[/b] extends vB_Api

      If it's done right, you'll see the separate PHP files listed as API classes under "List API Extensions and PHP Hooks"

      Code:
      Localapi_Api_JellyDonut
      [b]Localapi_Api_Lasagna[/b]
      Localapi_Api_Main

      In the user defined style template, it would be called like this:

      Code:
      {vb:data result, localapi:[b]lasagna[/b], itsHotAndMessy}
      {vb:raw result.html}

      This makes it a lot easier to debug when you have lots of functions... for our site we have what will work out to be around 30 or 40 custom functions. Being able to group them into category files has made it 10000% easier to debug as new functions are ported over.

      My suggestion is to add each function into "main.php" until it's debugged and solid, and then move the function into its eventual home.
    Posting comments is disabled.

About the Author

Collapse

Wayne Luke A curious juxtaposition of nature, technology and sustainability. Find out more about Wayne Luke

Article Tags

Collapse

administration (1) beginner (3) channels (1) cloud (1) connect (1) Custom (1) Google (1) how to (1) howto (1) https (1) Intermediate (2) MYSQL (1) passwords (1) recovery tools (1) security (2) seo (1) sftp (1) ssl (1) ssl certificate (1) style (1) tls (1) tutorial (2) users (1) vb5howto (5) vbcloud (1)

Latest Articles

Collapse

  • Changing your site's favicon
    by Wayne Luke
    The favicon allows you to extend your brand identity to the browser tab. This can often be the only thing a user sees if they have many tabs open. The favicon is a special 16 X 16 icon that the browser automatically looks for in the root directory of your site. vBulletin also ships with one in its root directory. If vBulletin is installed in the root directory of your site, you can overwrite the provided icon to change it for your users. However, you will need to remember to do this with every version...
    Tue 21 Nov '23, 11:39am
  • About .htaccess
    by Wayne Luke
    .htaccess is a configuration file for the Apache Web Server. It allows you to customize how your server works. Depending on how your hosting provider has the server configured, you can redirect people to different locations, secure directories and files, tell browsers how long to cache files, update PHP variables, and more. vBulletin comes with a pretty extensive .htaccess file that does a these things for you. Location vBulletin comes with a file named htaccess.txt in the original download. When ...
    Mon 11 Sep '23, 12:57pm
  • Answered Topics
    by Mark.B
    In vBulletin 5.6.5, we added a new feature - Answered Topics.

    Topics can now be marked as "Answered." This is a special status applied to a specific post within the topic. The post that is marked as the Answer will be highlighted and show directly under the starting post in the topic.

    The ability to mark posts as the Answer in a topic is controlled by permissions. There are three different permissions to control this.
    Usergroup and Channel Permissions both...
    Sun 8 Jan '23, 12:43pm
  • Custom Node Fields
    by Mark.B


    In 5.7.2, we have added a new feature - Custom Node fields.

    This new feature allows Administrators to define text and text area fields that are assigned to channels (eg forums).
    When a user creates a new topic, defined custom node fields will be available.
    1. The Administrator will create Field Categories in the AdminCP. Each category is assigned to one or more channels.
    2. Within each category, the Administrator can create one or more fields that will get assigned
    ...
    Thu 29 Dec '22, 3:19am
  • Using User Ranks
    by Wayne Luke

    User ranks allow the administrator to set up image and HTML rewards for their users once they reach specific goals. In versions before vBulletin Connect 5.7.1, ranks could only be triggered by the number of posts and the usergroups assigned to the user. In vBulletin Connect 5.7.1, User Ranks have been expanded to allow more flexibility in creating individual ranks. Now you can build combinations of different criteria to create unique ranks for your site.

    ​ Ranks can be created using th...
    Tue 25 Oct '22, 1:04pm
  • Excluding Custom Files from Suspect File Diagnostic
    by Wayne Luke
    If you have custom files, you can create an md5 sums for your smilie directory if you want. Inside the /do_not_upload folder of your download package is a checksum sub-folder. That will create custom md5_sums files.
    1. Copy the included sample_checksum_config.php file to smilies_config.php.
    2. Enter in your forum root.
    3. Give it a product id like 'smilies'.
    4. Delete the existing directories and files in the scanpaths.
    5. Add in '/core/images/smilies', under the directories comment.
    6. Run the command
    ...
    Mon 24 Aug '20, 9:48am
Working...
😀
😂
🥰
😘
🤢
😎
😞
😡
👍
👎