How to create a Hello World content plugin for Joomla 2.5

Joomla 2.5 has reached its end of life as for 12/31/2014. Please be advised this may be a security risk to your website. You can view more information about the end of life here.

In this first article of our tutorial series on how to create a Joomla 2.5 Content plugin, we will give you the source code for our Hello World plugin. This plugin will print “Hello World!” at the beginning of every article. As we dive further into more tutorials, we’ll provide more details on what all of this code means. For now we will show you how to create the plugin, how to install the plugin, and then what it does.

Step 1: Create the Plugin Files

All Joomla 2.5 plugins contain a xml file. These xml files contain information such as who wrote the plugin and when, what files should be included with the plugin, and any plugin settings that can be adjusted. The first thing you should do is copy the following text and save it as helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="content">
        <name>plg_content_helloworld</name>
        <author>Brad Markle</author>
        <creationDate>June 18th, 2012</creationDate>
        <copyright>InMotion Hosting</copyright>
        <license>GNU General Public License</license>
        <authorEmail>[email protected]</authorEmail>
        <authorUrl>https://www.inmotionhosting.com</authorUrl>
        <version>1.0</version>
        <description>Simple Hello World Plugin that prints "Hello World" at the beginning of every article.</description>
        <files>
                <filename plugin="helloworld">helloworld.php</filename>
                <filename>index.html</filename>
        </files>
</extension>

After creating the XML file, we now need to create our php file, which does all of the work. The following code should be saved to helloworld.php

<?php

// no direct access
defined('_JEXEC') or die;

class plgContentHelloworld extends JPlugin
{
    public function onContentAfterTitle($context, &$article, &$params, $limitstart)
        {
                return "<p>Hello World!</p>";
        }
}

?>

The last file we need to create is one named index.html. You don’t need to place any code in the file, you simply need to create it.

At this stage, you should have the following files:

  • helloworld.xml
  • helloworld.php
  • index.html

Compress all of these files into a zip file named helloworld.zip

Important! Keep in mind that Linux servers are case sensitive and helloworld.php is not the same as HelloWorld.php and so forth.

Step 2: Install the Plugin

Installing this plugin is like installing any other plugin. If you need any additional help, please see our article on how to install a Joomla 2.5 plugin.

Step 3: Enable and Test your new Plugin

When you initially install the plugin, it will be disabled, so be sure to enable the plugin.

hello-world-plugin-in-action

Visit any Joomla 2.5 article, and you should see “Hello World!” printed after the title, as in the following screenshot to the right.

Because we used the onContentAfterTitle event, our “Hello World” message is being shown on the content and after the title has been printed.

hide-show-intro-text

Please Note!
In order for this particular plugin to work, one more thing must be done. Because we’re using the onContentAfterTitle event, we need to set the article’s Show Intro Text value to Hide:

54 Comments

  • Ok,

    I got mine to work and figure out somethings that were not well explained in the joomla documentation it self.

    In my case the problem did lied in the plugin name, so I will write here what I expeienced and my undertanding of the beast.

    The Joomla docs show examples of plugin names like

    User – Hello World

    Content – Hello World

    The problem in my case was that I could not name the plugin like that, instead I had to name it plg_hello_world and then if I wanted to show a pretty name like the above I needed to create a language constant with the pretty name as value and load my plugin.

    I am guessing the reason for the plugin not working if you don’t name it like plg_group_name might be because somewhere in the framework something like(put on a simplistic way)

    $plgname = explode(“_”,”plg_group_name”);

    foreach( $plgname as $partname){

    echo ucfirst($partname);

    }

    This would echo the plugin name just like the class plgGroupName.

    While if we name our plugin Content – Hello World and the framework try to make sense of that, it will never match the class pattern.

    So in my case and in my opnion it is very importat we follow the correct name structure

    Please feel free to correct me if I am saying something worng here.

  • I am having the same problem and while trying to find an answer I came to this post and also found a lead https://stackoverflow.com/questions/26330012/joomla-plugin-event-not-firing

    I havent tested it yet, but seems the problem lies in language file

  • Hi Arn,

    Thanks for your reply,

    sorry for delay conversation

    I installed the helloworld plugin, after i enabled in administrator page, but it not display in the site , after the title, this was the problem,

    i followed the all steps in tutorial part

    • Hello Mathesh,

      What version of Joomla are you trying to make this plugin on? Keep in mind this article is for 2.5 and 3.x is almost completely different.

      Best Regards,
      TJ Edens

  • Hello john,

    thanks for your reply,

    in the administrator page only displays that helloworld ,

    but in the website page not displaying ,

    i changed settings show intro text->hide, but no use , article only displaying john….

    • Hello Mathesh,

      Sorry about the problems with the plugin. We can’t troubleshoot without knowing exactly what you’ve done in your code. Can you please provide us more information about the exact steps you have done so that we might try to duplicate the problem you’re having? Can you provide the URL or more information on your account (if you’re hosting with InMotion Hosting)?

      Provide us more specific information on the problem and we can try to look into it further.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  • Hi.

    Accidentally I updated updated my template.

    I now cannot not get the site displaying as expected. It is trancated with menues misplaced downwords instead of across. How do I revert back? I had not done a backup.

    Thanks

     

    • Hello Mathesh,

      Did you follow the directions exactly? Are you getting any errors on your home page when you try to call the helloworld plugin? What version of Joomla are you using?

      Best Regards,
      TJ Edens

  • please help me

    Fatal error: Cannot redeclare class plgContentHelloworld in C:\xampp\htdocs\f33\plugins\content\helloworld\helloworld.php on line 12

     

    • Hello Ravi,

      Sorry for the error that you’re seeing. We unfortunately do not support coding issues like this one. Also, this is a non-server related issue (since you’re using Xampp). You would be better supporter looking for the Xampp community. We do try to point you in the right direction as well. Check out this postg for further information.

      Regards,
      Arnel C.

  • I don’t see any error messages. I’ve just torn on all of error messages on hosting.

    My web-site is here https://j.novyna.info

    • Alex,

      We’d need to see more information on the error in order to figure out what’s happening. Can you provide the specific code from the file? And also the correct URL. The URL you provided (https://j.novyna.info) jumps to the Joomla page.

      Please provide this information and we can look into the issue further.

      Regards,
      Arnel C.

  • I couldn’t get this working either. (Joomla 3.3.3.) The plugin installed fine, I activated it but get nothing. Also endure that “show intro text” was set to hide on an article.
    Not sure what else to check – any ideas?

    Contents of the XML/PHP files below.

    <?php

    // no direct access
    defined(‘_JEXEC’) or die;

    class plgContentHelloworld extends JPlugin
    {
        public function onContentAfterTitle($context, &$article, &$params, $limitstart)
            {
                    return “<p>Hello World!</p>”;
            }
    }

    ?>

    <?xml version=”1.0″ encoding=”utf-8″?>
    <extension version=”3.3″ type=”plugin” group=”content”>
            <name>plg_content_helloworld</name>
            <version>1.0</version>
            <description>Simple Hello World Plugin that prints “Hello World” at the beginning of every article.</description>
            <files>
                    <filename plugin=”helloworld”>helloworld.php</filename>
                    <filename>index.html</filename>
            </files>
    </extension>

     

    • Hello DaveD,

      Are you getting any error message? Have you checked the error log in the files to see what type of error is occurring? Once you discover it, please reply with the full error message so we can take a look for you.

      Kindest Regards,
      Scott M

    • Hi Shailendra,

      Generally you get a headers already sent error when there is a space at the end of a php file.

      If the error tells you the exact file in question, look for the ?> at the end of the php file and make sure there is not a space after it.

    • Hello Shailendra,

      Thank you for your question. The headers already sent message is a common WordPress error the WordPress codex explains is “usually because there are spaces, new lines, or other garbage before an opening tag, typically in wp-config.php.

      See the full cause and solution to the headers already sent problem in the Official WordPress documentation.

      If you have any further questions, feel free to post them below.
      Thank you,

      -John-Paul

  • At the time of plugin active i got this message so how can I over come it ?

     

    The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

  • I am using joomla 3.1 but still Hello world not showing up

    I  followed all the steps 

    Please Help

    • Be sure that you have placed the entire code mentioned in the above article into the appropriate files. Once all of this has been checked over, ensure that your plugin is active, as well as the intro text is disabled as described within the article.

  • I know why, because i using joomla 2.5 but “onContentAfterTitle” function only avalible on joomla 3.x.

    I just start using joomla, i want to learn how to develop a plugin and found this site.. Thanks for sharing!

    • Hello Sang,

      You’re right that the onContentAfterTitle function does not exist in Joomla 2.5 by default. If you’re following the steps of this guide, you actually are creating that function for Joomla 2.5 to use.

      You might want to take a look at my earlier comment about creating the onContentAfterTitle function, and the next few comments after that, as they explain a bit more than the guide itself about this.

      Let us know if you had any other questions!

      – Jacob

  • Hi, thanks for the sample plugin.

    But i have a problem : i installed and actived plugin successfull but not display anything, i try to replace “return” to “echo” so “Hello World!” display on top of title . Please help! Thanks.

    • We have tested this code to ensure that it does work on Joomla 2.5. Be sure that you are running Joomla 2.5 and not another version as well as check the error logs on the server for any possible errors.

      If you’re still having trouble, you may send an email to doc[email protected] with your Joomla credentials and we will be happy to take a look for you. Be sure to reply to this comment if you send us an email.

Leave a Reply to InMotionFan Cancel reply