Joomla plugin to insert codes in head section


Following is a template that can be used to develop plugins to inert code into Joomla head section.

01.<?xml version="1.0" encoding="utf-8"?>
02.<install version="1.5" type="plugin" group="system">
03.<name>System - Name of Plugin</name>
04.<author>Name of  author</author>
05.<creationDate>19th April 2010</creationDate>
06.<authorEmail>
07.<script language="JavaScript" type="text/javascript">
08.<!--
09.var prefix = 'mailto:';
10.var suffix = '';
11.var attribs = '';
12.var path = 'hr' + 'ef' + '=';
13.var addy40766 = 'email' + '@';
14.addy40766 = addy40766 + 'email' + '.' + 'com';
15.document.write( '<a ' + path + '\'' + prefix + addy40766 + suffix + '\'' + attribs + '>' );
16.document.write( addy40766 );
17.document.write( '<\/a>' );
18.//-->
19.</script><a href="mailto:email@email.com">email@email.com</a><script language="JavaScript" type="text/javascript">
20.<!--
21.document.write( '<span style=\'display: none;\'>' );
22.//-->
23.</script><span style="display: none;">This e-mail address is being protected from spambots. You need JavaScript enabled to view it
24.<script language="JavaScript" type="text/javascript">
25.<!--
26.document.write( '</' );
27.document.write( 'span>' );
28.//-->
29.</script></span></authorEmail>
30.<authorUrl>http://www.khawaib.co.uk</authorUrl>
31.<version>1.0</version>
32.<copyright>Copyright (c) 2010 Khawaib Ahmed. All rights reserved.</copyright>
33.<license>http://www.gnu.org/copyleft/gpl.html GNU/GPL</license>
34.<description> Plugin description.</description>
35.<files>
36.<filename plugin="phpFileName">phpFileName.php</filename>
37.</files>
38.<params>
39.<param name="code" type="text" default="[DEFAULT VALUE]" label="[LABEL]" description=" [DESCRIPTION]"  />
40.</params>
41.</install>
01.<?php
02.// no direct access
03.defined( '_JEXEC' ) or die( 'Restricted access' );
04.
05.jimport( 'joomla.plugin.plugin');
06.
07.class plgSystemAsynGoogleAnalytics extends JPlugin {
08.function plgAsynGoogleAnalytics(&$subject, $config)   {
09.parent::__construct($subject, $config);
10.$this->_plugin = JPluginHelper::getPlugin( 'system', 'AsynGoogleAnalytics' );
11.$this->_params = new JParameter( $this->_plugin->params );
12.}
13.
14.function onAfterRender() {
15.global $mainframe;
16.
17.// skip if admin page
18.if($mainframe->isAdmin()) {
19.return;
20.}
21.
22.// get params
23.$trackerCode = $this->params->get( 'code', '' );
24.
25.//getting body code and storing as buffer
26.$buffer = JResponse::getBody();
27.
28.//embed Google Analytics code
29.$javascript = "<script type=\"text/javascript\">
30.var _gaq = _gaq || [];
31.
32._gaq.push(['_setAccount', '".$trackerCode."']);
33._gaq.push(['_trackPageview']);
34.
35.(function() {
36.var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
37.ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
38.var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
39.})();
40.</script>";
41.
42.// adding the Google Analytics code in the header before the ending </head> tag and then replacing the buffer
43.$buffer = preg_replace ("/<\/head>/", "\n\n".$javascript."\n\n</head>", $buffer);
44.
45.//output the buffer
46.JResponse::setBody($buffer);
47.
48.return true;
49.}
50.}
51.?>

Bình luận về bài viết này