Search code examples
modulesmartyadd-onheadwhmcs

How can i add lines to WHMCS Admin Addon Module Template {headoutput}?


First of all, I am new here and my English is not good enough so i am sorry about that. Here is my question;

I am trying to code an addon module for WHMCS 5 and i want to use bootstrap modal windows in my addon's pages. I am trying to add css and js lines in to the main page of my addon. I want to put them inside the head tag by coding, i do not want to change the template file and i know that it is possible to do this because "WHMCS's Project Planner" module is doing this successfully. I can see that in the source codes of pages.

When i looked into the admin template's "header.tpl" file, there are two smarty codes. {headoutput} and {headeroutput}. They are positioned like below;

<head>
  {headoutput}
</head>
<body>
  {headeroutput}

I have made lots of research about how to send my code to {headoutput} by my module .php file. If anybody knows how to do this, please i need help very much.

Your help is very appreciated. Best Regards, Caner


Solution

  • I have found the solution for reading more documents in WHMCS Documentation and some other places.

    First of all as i learnt, after WHMCS 4, developers changed some rules and WHMCS's {headoutput} and {headeroutput} smarties in the templates are not directly editable by the code anymore. WHMCS uses Hooks for adding user codes between special html blocks. And here is the solution about my problem.

    Doc. Ref. 1: http://docs.whmcs.com/Hooks
    Doc. Ref. 2: http://docs.whmcs.com/Hooks:System

    After we did the essential things to create an addon module, we are creating a new file in the module folder named "hooks.php". WHMCS is including this file directly, without any decleration to include, in to our module. In the "hooks.php" we need to create a hook function, explained as in the Doc. Ref. 1. Your function does not need to have a special name but WHMCS developer advices you to use a hierarchy like starting with "hook_", followed by the filename, and then the particular action or task that hook is performing. For example;

    function hook_youraddonmodule_adminHeadBlock($vars) {
        //Here is my custom code below
        $headoutput = '<link href="../includes/jscript/css/bootstrap.min.css" rel="stylesheet">';
        return $headoutput;
    }
    

    After you name the function, you must type your code between the curly braces that according to your needs. I put bootstrap css file link that i wanted to be included by all my module pages' head blocks. We declared a hook function and then we need to prepare it to be used by WHMCS's predefined hook places as explained int the Doc. Ref. 2. To put the code between head block we have to use "AdminAreaHeadOutput" hook decleration. Here is the code that i used;

    // Define Client Login Hook Call
    add_hook("AdminAreaHeadOutput",1,"hook_youraddonmodule_adminHeadBlock");
    

    This definition means that we have added a hook to admin area head output and it's priority is first (1). Actually i do not know why we need a priority about hooks and i did not made a research about it but if it has done, there must be an advantage of it :) Now all you need to save the hook file and upload it to your server. All done. I hope it helps the people who is looking for this kind of tricks.

    Update:

    I have recognized that, when you create a hook and activate it with your module in your admin area, it applies to all admin pages so it can break some of whmcs's standart javascripts and styles. If you want the hook only work for your own module, you should add a code like below for preventing your own codes to be applied in all admin area pages.

    $moduleName = $_GET['module'];
    if($moduleName == "yourmodulename"){
        return $headoutput;
    }
    

    Regards,
    Caner.