Knowledge.ToString()

Create Joomla Extension to Show Hello World on Every Page

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

Note: Bold named folder/file must exist in order for the plugin to function.

Create a folder JOOMLA_ROOT/plugins/system/pramukhime (Why here? because it is a system plugin called PramukhIME)

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">
    <!-- This name tag is a user friendly name and you can name it the way you want. -->
    <name>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>
        <!-- plugin tag value must be plugin name and the php file must be the pluginname.php -->
        <filename plugin="pramukhime">pramukhime.php</filename>
    </files>
</extension>

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.

Share

Comments

One response to “Create Joomla Extension to Show Hello World on Every Page”

  1. Richard Townsend-Rose Avatar
    Richard Townsend-Rose

    Starting my first plugin …
    This looks ace.
    Eventually I want to display a kml file using google maps.
    richard

Leave a Reply

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