Creating Pages

From WHMCS Documentation

Revision as of 16:09, 15 October 2016 by Matt (talk | contribs)

Standard Page

To get started, copy & paste the code below into a new PHP file and save it in the main WHMCS directory.

<?php

use WHMCS\Database\Capsule;

define("CLIENTAREA", true);
//define("FORCESSL", true); // Uncomment to force the page to use https://

require("init.php");

$ca = new WHMCS_ClientArea();

$ca->setPageTitle("Your Page Title Goes Here");

$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
$ca->addToBreadCrumb('mypage.php', 'Your Custom Page Name');

$ca->initPage();

//$ca->requireLogin(); // Uncomment this line to require a login to access this page

# To assign variables to the template system use the following syntax.
# These can then be referenced using {$variablename} in the template.

$ca->assign('variablename', $value);

# Check login status
if ($ca->isLoggedIn()) {

  # User is logged in - put any code you like here

  # Here's an example to get the currently logged in clients first name

  $clientName = Capsule::table('tblclients')
      ->where('id', '=', $ca->getUserID())->pluck('firstname');
      // 'pluck' was renamed within WHMCS 7.0.  Replace it with 'value' instead.
      // ->where('id', '=', $ca->getUserID())->value('firstname');

  $ca->assign('clientname', $clientName);

} else {

  # User is not logged in

}

# Define the template filename to be used without the .tpl extension

$ca->setTemplate('mypage');

$ca->output();

This example above shows you the required layout of a custom page. It demonstrates:

  • How to initiate the page
  • How to reference the language file variables (Lang::trans)
  • How to check if a user is logged in ($ca->isLoggedIn())
  • How to define template variables ($ca->assign)
  • How to set the template to use and then output it

The template file should be a filename in the active WHMCS system template folder. So, for the example above the path would be /templates/default/mypage.tpl.

Now when ready to test, upload the PHP file to the root WHMCS directory and the template file to your active template directory. Then visit the PHP file in your browser to try it.

Custom pages created in this way should always be located in the root WHMCS directory. Attempting to use this code outside of the root directory may lead to issues and is unsupported.