Difference between revisions of "Creating Pages"

From WHMCS Documentation

(Standard Page)
Line 3: Line 3:
 
==Standard Page==
 
==Standard Page==
  
To get started, simply copy & paste the code below into a new PHP file and save it in the main WHMCS directory.
+
To get started, copy & paste the code below into a new PHP file and save it in the main WHMCS directory.
 
 
<div class="docs-alert-info">Compatible with WHMCS 5.2 and later (including 6.0)</div>
 
  
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
 +
use WHMCS\Database\Capsule;
  
 
define("CLIENTAREA", true);
 
define("CLIENTAREA", true);
Line 19: Line 19:
 
$ca->setPageTitle("Your Page Title Goes Here");
 
$ca->setPageTitle("Your Page Title Goes Here");
  
$ca->addToBreadCrumb('index.php', $whmcs->get_lang('globalsystemname'));
+
$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
 
$ca->addToBreadCrumb('mypage.php', 'Your Custom Page Name');
 
$ca->addToBreadCrumb('mypage.php', 'Your Custom Page Name');
  
Line 38: Line 38:
 
   # Here's an example to get the currently logged in clients first name
 
   # Here's an example to get the currently logged in clients first name
  
   $result = mysql_query("SELECT firstname FROM tblclients WHERE id=" . $ca->getUserID());
+
   $clientName = Capsule::table('tblclients')->where('id', '=', $ca->getUserID())->pluck->('firstname');
  $data = mysql_fetch_array($result);
 
  $clientname = $data[0];
 
  
 
   $ca->assign('clientname', $clientname);
 
   $ca->assign('clientname', $clientname);
Line 61: Line 59:
 
*How to initiate the page
 
*How to initiate the page
 
*How to force the page to use SSL (FORCESSL)
 
*How to force the page to use SSL (FORCESSL)
*How to reference the language file variables ($whmcs->get_lang)
+
*How to reference the language file variables (Lang::trans)
 
*How to check if a user is logged in ($ca->isLoggedIn())
 
*How to check if a user is logged in ($ca->isLoggedIn())
 
*How to define template variables ($ca->assign)
 
*How to define template variables ($ca->assign)
*And how to set the template to use and then output it
+
*How to set the template to use and then output it
  
The template file you define should be a filename in your active WHMCS system template folder, so for the example above the path would be /templates/default/mypage.tpl
+
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 you're ready to test, upload both the PHP and TPL file to the appropriate folders and then visit the PHP file in the root WHMCS directory to run.
+
Now when ready to test, upload both the PHP and TPL file to the appropriate folders. Then visit the PHP file in the root WHMCS directory to run.
  
 
==Page with a Sidebar==
 
==Page with a Sidebar==

Revision as of 10:58, 5 January 2016

Creating your own custom pages for WHMCS is simple. Use this to extend the client area. For example, building the whole site around WHMCS, or creating custom pages to provide extra functionality to clients.

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');

  $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 force the page to use SSL (FORCESSL)
  • 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 both the PHP and TPL file to the appropriate folders. Then visit the PHP file in the root WHMCS directory to run.

Page with a Sidebar

In version v6.0 and later is it possible to define a sidebar to be displayed on the custom page. The following example shows how to create a custom page as above, and also define a sidebar to be displayed alongside, it demonstrates:

  • How to set contact for the sidebar, thereby influencing what data is passed to the menu
  • How to define both the primary and secondary sidebars for display
<?php

use WHMCS\ClientArea;
use Illuminate\Database\Capsule\Manager as Capsule;


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

require __DIR__ . '/init.php';

$ca = new 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
     */

    $client = Capsule::table('tblclients')->where('id', $ca->getUserID())->first();
    $ca->assign('clientname', $client->firstname);

} else {

    // User is not logged in
    $ca->assign('clientname', 'Random User');

}

/**
 * Set a context for sidebars
 *
 * @link http://docs.whmcs.com/Editing_Client_Area_Menus#Context
 */
Menu::addContext();

/**
 * Setup the primary and secondary sidebars
 *
 * @link http://docs.whmcs.com/Editing_Client_Area_Menus#Context
 */
Menu::primarySidebar('announcementList');
Menu::secondarySidebar('announcementList');

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

$ca->setTemplate('mypage');

$ca->output();