I'm trying to write a Joomla plugin that injects a small javascript code into all pages for distribution to others. but everything I have read says I should do it using the $doc = JFactory::getDocument();
but I can't get it to work.
This is the code I'm trying to use and Joomla says everything is ok with it but once activated if I go to the homepage of my site my snippet of javascript is not being added to the source of the page.
Current Code:
bwai.xml
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="system" method="upgrade">
<name>Beacon Web Analytics Intergration for Joomla</name>
<creationDate>June 2018</creationDate>
<author>Beaconsoft Limited</author>
<authorEmail>contact@thisisbeaon.com</authorEmail>
<authorUrl>https://www.thisisbeacon.com</authorUrl>
<copyright>Beaconsoft Limited 2018</copyright>
<license>BSD new</license>
<version>1.0.0</version>
<description>This plugin enables the Beacon Web Analytics Intelligence to run on your site, this information will not be saved unless you have an account for Beacon.</description>
<files folder="site">
<filename plugin="bwai">index.php</filename>
<filename>index.html</filename>
</files>
<administration>
</administration>
</extension>
index.php
<?php
defined('_JEXEC') or die('No direct access');
die("hello");
jimport('joomla.plugin.plugin');
class plgSystemCustomhead extends JPlugin{
function onBeforeCompileHead() {
die("hello hello");
/*if (JFactory::getApplication()->isAdmin()){
return true;
}*/
$doc = JFactory::getDocument();
$doc->addScriptDeclaration('
(function(a, d, w){
var h= d.getElementsByTagName(a[0])[0]; var s= d.createElement(a[1]);
s.setAttribute("type", a[2]); s.setAttribute("src", a[3]); s.setAttribute(a[4], true); s.setAttribute(a[5], a[6]); h.appendChild(s);
})(["head", "script", "text/javascript", "//tracker.thisisbeacon.com/tracker/", "async", "rel", "preload"], document, window);
', 'text/javascript');
}
}
?>
All I want to do is have a small plugin people can install that adds our tracking javascript snippet code to all pages on the front end of a Joomla site.
Adding exit('hello hello');
to the top of the function i have also tried adding die("hello");
to the top of the file under the defined or die statement it does not execute but then Joomla reports the extension as active, as you can see above
rename your index.php to bwai.php, and in xml, write
<filename plugin="bwai">bwai.php</filename>
then your class name should match the plugin name, and be PlgSystemBwai