Knowledgebase: Design Templates
Integrate Custom PHP Code
Posted by - NA -, Last modified by Mark [Elevated X Support] on 01 March 2018 05:29 PM

Integrate Custom PHP Code

Note - This is not part of our standard installation. Use of our tech support staff for assistance with custom coding will result in additional support charges.

Sometimes, a custom piece of PHP code needs to be integrated into the CMS.  Here is how you can set this up.

Step 1) Go to the file cmsinclude.ini.php.  Under the requires tab, you'll see a list of files that are loaded when a user accesses the members area.  These files are part of the CMS, but you can add to them:

require[] = "trial.php"
require[] = "$_SERVER[DOCUMENT_ROOT]/cms_admin/includes/alwaysexec.php"
require[] = "$_SERVER[DOCUMENT_ROOT]/cms_admin/includes/membersMain.class.php"
Under require3, you can add your own lines that will include a PHP file:
require[] = "myfile1.php"
require[] = "myfile2.php"
Note: Older installs may have lines that use require1, require2, require3, like so:
require1 = "trial.php"
require2 = "$_SERVER[DOCUMENT_ROOT]/cms_admin/includes/alwaysexec.php"
require3 = "$_SERVER[DOCUMENT_ROOT]/cms_admin/includes/membersMain.class.php"
Regardless of if you see this, we recommend you still use require[] for any adding new lines instead of the numerical version.

Step 2) Create the file myfile1.php and put in any custom code that's needed.

Altering the output from the CMS

Sometimes, a custom piece of PHP needs to alter the output from the CMS.  Here's how this can be done:

Step 1) Go back to the file cmsinclude.ini.php.  Under the flags tab, you'll want to add the following:
 POSTPROCESS=myFunction

What this does is tell the CMS to load a function named "myFunction" after the template output has been generated.

Step 2) Within myfile1.php (or whatever filename you added to the system up above), add the following function

function myFunction($input)
{
  $output = $input;

  // $input is the HTML code generated after Smarty does its render process.

  // Your custom code will go here
  // Example: This will replace any instance of ##randomnumber## in the template with a random number.
  // $output = str_replace("##randomnumber##", mt_rand(), $output);

  return $output;

  // return $output, which is modified $input. This is what is displayed on screen to the end user.
}

Note - The name of the function you'll need to do is based on what you put within the POSTPROCESS setting.  So if you set the POSTPROCESS flag to "Custom", then you will need to call your custom function "Custom"

Note - As of build 3280, multiple functions are supported.

Example:

POSTPROCESS=customfunction1,customfunction2,customfunction3

 

Adding Smarty Variables to the CMS

Sometimes, custom smarty variables need to be assigned within the system

Step 1) Go back to the file cmsinclude.ini.php.  Under the flags tab, you'll want to add the following:
SMARTY_CUSTOMIZE_VARIABLES=variable_append

What this does is tell the CMS to load a function named "variable_append" after the template output has been generated.

Step 2) Within myfile1.php (or whatever filename you added to the system up above), add the following function

function variable_append(&$smarty)
{
  //Your custom code will go here
  // Example: This will assign a variable named $test with the value "This is a test" (not in quotes).
  // $smarty->assign("test", "This is a test");

}

Note - The name of the function you'll need to do is based on what you put within the SMARTY_CUSTOMIZE_VARIABLES setting.  So if you set the SMARTY_CUSTOMIZE_VARIABLES flag to "Custom", then you will need to call your custom function "Custom"

Note - Multiple functions are allowed here.

Example:

SMARTY_CUSTOMIZE_VARIABLES=customfunction1,customfunction2,customfunction3

(8 vote(s))
Helpful
Not helpful

Comments (0)