Tutorial to create a Simple Joomla Plugin

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

For the purpose of this tutorial example, we will create a Joomla plugin that will alter the title tag of pages of a Joomla 1.5 website.

The text surrounded by the title tags in the HTML code is what the browser will display in the browser window frame and browser tabs.

Joomla title tag plugin

Joomla title tag

By default, Joomla sets the title tag to be the title of the article being displayed. We want to create a plugin that will append our site name at the end of the title tag as in …

sitename added to title tag

sitename added to title tag

For the purposes of this example, the site name is “Demo Joomla Site”.

Do Not Modify Core Joomla Code

The core Joomla code that controls the output of the title tag is found in …
libraries/joomla/document/html/renderer/head.php

However, it is not considered good practice to alter this file here. Because an Joomla upgrade may wipe out your changes to that file. Altering core Joomla files to get the behavioral changes you want is sometimes known as a “hack”.

Better to Create a Joomla Plugin

Instead, a better way is to create a Joomla plugin that will add our site name to this output without modifying the Joomla core code. We will be inserting new files into the Joomla system instead of altering any existing Joomla files. We will call our plugin “SiteNameInTitle”.

1. Plugins can be in any of the nine plugin groups shown below in the Joomla file system.

Joomla plugin groups

Joomla plugin groups

We will put our plugin into the “system” group. So we create two files (sitenameintitle.xml and sitenameintitle.php within the system folder on our local computer (not on the webhost yet). I like to keep all the characters of the filename in lowercase.

2. The sitenameintitle.xml is the plugin installer file. Make it look similar to the following with your own relevant information.

Joomla plugin xml file

Joomla plugin xml file

The name of our plugin is highlighted in the above. Also note that we put group=”system” and the reference to our second file sitenameintitle.php

3. For our second file sitenameintitle.php, we put the following …

Joomla plugin code

Joomla plugin code

We create a class plgSystemSiteNameInTitle which extends JPlugin. The class name is constructed by convention to start with plg to indicate a plugin followed by the plugin group and by the plugin name.

4. Inside the class, we have a function onAfterDispatch() to handle the event.

First we get our document object by …
$document = & JFactory::getDocument();
We need this to access our title.

We handle the event onAfterDispatch by setting the document title to whatever the document title is currently suffixed by the site name …

$document->setTitle($document->getTitle() . ‘ – ‘ . $mainframe->getCfg(‘sitename’));

We use $document->getTitle() to get our current title. We concatenate it with a hyphen. And we get the site name as configured in the Joomla administrator via $mainframe->getCfg(‘sitename’)

Then we use $document->setTitle to set the new title.

5. Now zip the two files up into a zip file called sitenameintitle.zip

6. In your Joomla Administrator -> Extensions -> Install, browse to pick up the sienameintitle.zip and click Upload File and Install

upload and install plugin

upload and install plugin

7. You should see “Install Plugin Success”. Note the plugin description from the XML file is also shown here.

Joomla plugin successful

Joomla plugin successful

Joomla has just unzipped the two files sitenameintitle.xml and sitenameintitle.php into the plugins/system folder on your webhost.

8. In Extensions -> Plugin Manager, find your new plugin and enable it.

enable Joomla plugin

enable Joomla plugin

9. Refresh your browser and see that site name is now added to the title tag…

sitename added to title

sitename added to title

Notes:

We can not remove the text “Mozilla Firefox” that you see in the browser frame because that is put there by the browser and not controlled in the title tag HTML code.

This tutorial refers to Joomla version 1.5.14.

 


Tags

Share This