Create Joomla extension to show Hello World on every page

By | October 25, 2017

I wanted to create a simple Joomla 3.x plugin to show HTML on every page including the administration pages. It was extremely difficult to find any sample online as I did not know the what to look for. Here is a very quick tutorial on how to show “Hello World” on each and every Joomla page including administration pages.

Prerequisites

Joomla 3.x

JOOMLA_ROOT is the root folder where Joomla is installed.

Quick Hint: We are going to create system plugin (system = a type of plugin group that runs for every page, plugin = a type of Joomla extension).

Plugin Name: PramukhIME

Steps

  1. Create a folder JOOMLA_ROOT/plugins/system/pramukhime (Why here? because it is a system plugin called PramukhIME)
  2. Create a file JOOMLA_ROOT/plugins/system/pramukhime/pramukhime.xml and put the following code in it
    <?xml version="1.0" encoding="utf-8"?>
    <extension version="3.1" type="plugin" group="system" method="upgrade">
    	<name>plg_system_pramukhime</name>
    	<author>Vishal Monpara</author>
    	<creationDate>October, 2017</creationDate>
    	<copyright>Copyright (C) 2017 Vishal Monpara. All rights reserved.</copyright>
    	<license>see license.html</license>
    	<authorEmail>myemailaddress@something.com</authorEmail>
    	<authorUrl>www.vishalon.net</authorUrl>
    	<version>1.0.0</version>
    	<files>
    		<filename plugin="pramukhime">pramukhime.php</filename>
    	</files>
    </extension>
    
  3. Create a file JOOMLA_ROOT/plugins/system/pramukhime/pramukhime.php and write following code in it
    <?php
    /**
     * @package     Joomla.Plugin
     * @subpackage  System.PramukhIME
     *
     * @copyright   Copyright (C) 2017 Vishal Monpara. All rights reserved.
     * @license     see license.html
     */
    
    defined('_JEXEC') or die;
    
    class PlgSystemPramukhIME extends JPlugin
    {
    	protected $app;
    
    	public function onAfterDispatch()
    	{
    		// Uncomment following line to add your own css/javascript file in head
    		//JHtml::_('stylesheet', 'media/path/to/pramukhindic.css', array('version' => 'auto', 'relative' => false));
    		//JHtml::_('script', 'media/path/to/pramukhime.js', array('version' => 'auto', 'relative' => false));
    	}
    	
    	public function onAfterRender()
    	{
    		// Get page html which includes full page
    		$body = $this->app->getBody();
    		$content = "<h1>Hello World</h1>";
    		// locate </body> tag and insert our own content right before </body>
    		$body = str_replace('</body>', $content . '</body>', $body );
    		// set the body
    		$this->app->setBody($body);
    	}
    }
    
    

That’s it. You have made your first system plugin.

Installation

  1. Login to the Joomla Administration
  2. Go to Extensions (top menu) > Manage > Discover
  3. It will list the plugin as plg_system_pramukhime
  4. Select the checkbox for this plugin and click “Install” on the top left corner.
  5. So far the plugin is installed but yet to be enabled.
  6. Go to Extensions > Plugins
  7. Search for pramukhime and it will show plg_system_pramukhime
  8. Click on the box under “Status” to enable the plugin
  9. As soon as you enable it, you will see Hello World in the bottom.
  10. Go to the front page and you will also see Hello World in the bottom.
Vishal Monpara is a full stack Solution Developer/Architect with 12 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1’s and 0’s from geographically dispersed hard disks to geographically dispersed user’s mind leveraging geographically dispersed team members.

Leave a Reply

Your email address will not be published. Required fields are marked *