Search code examples
phphookwhmcs

How can i add a hook for whmcs blog


I have installed whmcs and then added an addon called Simple Blog to whmcs. I want to add a whmcs hook in order to get an image from the database.

I have the PHP code to get the image from the database but I don't know how to create a hook and where do hooks located.

I can use already available hooks inside .tpl files but I don't know how to create a hook.

I want this code to go in the hook should accept one argument which is the id of the blog then the code will return the image currently, this core returns the image but it's not a hook and I cannot use it inside the .tpl files

$query = "SELECT image FROM mod_blog_posts WHERE id='$id'"; 
$result = mysql_query($query);

while ($data = mysql_fetch_array($result)) {
  $image = $data['image'];
}

Kindly help


Solution

  • I used smarty to get the work done i created a smarty plugin by simply going to vendor/smarty/smarty/libs/plugins

    then i created a file there and named it function.getblogimage.php

    the code inside this file is

    <?php
    /**
     * Smarty plugin
     *
     * @package    Smarty
     * @subpackage PluginsFunction
     */
    /**
     * Smarty {getblogimage} function plugin
     * Type:     function
     * Name:     getblogimage
     * Purpose:  print out a blog image
     *
     * @author Kode Sensei
     *
     * @param array                    $params   parameters
     * @param Smarty_Internal_Template $template template object
     *
     * @return string|null
     */
    function smarty_function_getimagealam($params, $template)
    {
    
    $query = "SELECT image FROM mod_blog_posts WHERE id=".$params[ 'id' ]; 
    $result = mysql_query($query);
    
    while ($data = mysql_fetch_array($result)) {
      $image = $data['image'];
      return $image;
     }
    }
    
    ?>
    

    Now i can use this plugin inside the .tpl files like this {getblogimage id=$id} It accepts a parameter id and by giving the blog post id it retrieves the blog image.