¡Hi!

I'm new in the forum, but a 3 year user of osTicket, ¡great software!

This year, I started making plugins, now I have new requiremente (already solved) and I want to know if is a better alternartive for what I just do.

I need a plugin to add javascript to all pages in public area (client side). Specifically, this plugin was for adding a chat to the support page. But the same idea apply for new plugins that need the same, example: for adding a Facebook Pixel or a Google Analytics code to the support public page.

I just want that, a plugin for adding script in head tag. I don't want to modify the layout or anything that is replaceble with an update, that is why I want a Plugin.

I was unable to find the way to add just code to the head from a plugin.

First aproach: in include/class.osticket.php exist a method called getExtraHeaders but doesn't work inside Plugin::bootstrap(), because $ost global variable is not yet available.

Second aproach: taking the idea of getExtraHeaders I made two modifications in osTicket core:

  1. In include/class.plugin.php I added this to PluginManager:
function getExtraHeaders() {
    $headers = [];
    foreach ($this->allActive() as $p) {
        if (method_exists($p, 'getExtraHeaders')) {
            $headers = array_merge($headers, $p->getExtraHeaders());
        }
    }
    return $headers;
}
  1. In include/client/header.inc.php I added this inside the head tag:
if ($ost && ($headers=$ost->plugins->getExtraHeaders())) {
    echo "\n\t".implode("\n\t", array_map(function($h) {return str_replace("\n", "\n\t", $h);}, $headers))."\n";
}

Then, in my Plugin I just create the method getExtraHeaders and it's works. I have de script inside the header tag and the chat shows up.

Now my question: Is any another way of doing this? Maybe I miss something. Another class or feature that can be used?

If is no other way of doing this, is this change suitable for a pull request? I don't want to do it before asking for a third aproeach here 🙂

For now, is working what I want, but I prefeer use a osTicket core functionalitty without making any changes to the core files.

Thanks!

PavelH

Yes, I can use include, but the idea is a general solution and in osticket core, without me changing the core files.

Maybe doesn't exist and the only aproach for now is modyfing the osticket core files.

AdamDempsey

That is another approach. I searched for a signal, but in that moment, I thinked it was more "clean" use a plugin method.

But, signals are a great options. I just leaved a support vote in the pull request. I hope the team include the PR or something similar.

2 years later

I created a plugin to do just this, but there's no 'clean' way of getting access to$ost->addExtraHeader() from within the plugin itself.

a year later

Hi ! Can I ask you to share the plugin to install it in our OsTicket ? Or we just need to add the script code above to add it ? Does the script above works with OsTicket 1.18.1 ? Because I add the code in the 2 pages mentioned and it doesn't work for me. Thanks in advance ?

    Write a Reply...