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>bradm@inmotionhosting.com</authorEmail>
        <authorUrl>http://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-actionVisit 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-textPlease 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:

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve our Support Center:
Email Address
Optional, but our team may contact you for more information.
Like this Article?

Comments

Post a comment
2013-09-18 10:38 pm
good article. thanks so much
i create one simple extension in my site,here: http://vnlance.com
2013-11-08 6:39 am
how can i create system plugin i am trying but it gives me error and website is not load plugin is on my local website please give me reply on rt87.shrivas@gmail.com
Staff
4,058 Points
2013-11-08 9:31 am
Hello arti145,
Could you clarify what specific error you are getting?
2013-11-13 12:09 pm
I followed the instructions, but it make no changes to the content. Please help.
Staff
10,559 Points
2013-11-13 2:10 pm
Hello Hassanz,

We need more information on the issue in order to help you, please. Can you please provide a URL, version and description of how to duplicate the problem you are having?

Regards,
Arnel C.
2013-11-15 12:50 pm
Hello Arn,
There is no url its on local server. I am using Joomla 2.5 with no other plugin. According to this guide (http://docs.joomla.org/Plugin/Events/Content) onContentAfterTitle is only available in J3.
Staff
5,587 Points
2013-11-15 1:53 pm

Hello hassanz,

I just tested this out and it works perfectly fine in Joomla 2.5. While the onContentAfterTitle function is only available out of the box in Joomla 3.0, when you're creating the helloworld.php file mentioned in this article, you're creating that same function with this line of code:

public function onContentAfterTitle($context, &$article, &$params, $limitstart)


Make sure that you're looking at an article after using this plugin, and also make sure that you've set the Show Intro Text value to Hide otherwise the article's default intro text will override the plugins text.

- Jacob
2013-11-15 2:27 pm
okay, but even if you get it to work how do you expect one to edit 100s of article and change SHOWintroTEXT to hide ? I did not find any Global option for that.
Staff
5,587 Points
2013-11-15 3:32 pm
Hello hassanz,

Thie tutorial simply covers how to create a Hello World plugin for Joomla so that you understand how Joomla plugins work.

You can also simply set this option globally if you'd like as well, by going to Content > Article Manager and then clicking on Options. Then simply select Hide beside the Show Intro Text option:

joomla global show intro text

- Jacob
2013-11-15 5:44 pm
ok make sense. I have another question. How can you prevent showing "Hello World" on category page (a page where multiple article's snippet exists. But instead it should appear only when user is viewing Single Article only.
Staff
5,425 Points
2013-11-18 12:43 pm
Hello hassanz,

You can choose, which pages to assign a module to in the Admin Dashboard:
  1. Login to the Dashboard.

  2. Roll your mouse over Extensions, and click Module Manager.

  3. Click the Module you want to assign to pages.

  4. In the Menu Assignment field, choose Only on the pages selected, then you can Select the pages you want to assign the plugin/module to

  5. Click Save when you are finished.


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

-John-Paul
2013-11-18 12:52 pm
plugin we created above does not appear in Module Manager.
Staff
5,587 Points
2013-11-18 2:53 pm
Hello hassanz,

Sorry for the confusion, as this is a Plugin and not a Module it would instead be found in Extensions > Plug-in Manager. However this plugin does not have the option to only display on single Article pages only.

Again this plugin was not intended for actual real world use, but rather demonstrating how the Joomla plugin system works. For a more in-depth look about Joomla Plugins you'd want to check out the official Joomla Plugin Development page for help.

- Jacob
2013-11-18 2:59 pm
Hi JacobMH,
Thanks for taking time and answering my question. I understand that it was not meant for production use but I am now in middle of creating a production plugin using the basic concept of this tutorial. (That is why I am keep coming back here for help). Unfortunately, the link you gave me is useless. I know that it is official Dev docs by Joomla but it does not cover everything. If you can guide me or point me to right direction I might be able to figure it out myself. You are more responsive than any other Joomla website I have found so far. Thanks again for all the help.
Staff
2,760 Points
2013-11-18 3:18 pm
Hi hassanz,

It looks like you've posted several questions, but your current question is how you can prevent this plugin from showing on categories?

We may need a screenshot for further help with understanding what exactly is happening, but I'll try to help first without any additional information.

Within your code, determine if you're looking at an article

Within your code, you can ask Joomla to tell you whether you are looking at an article or a category. You can then use that information to enable / disable the plugin.

The following code has been tested with Joomla 3.0 and a module, but it may work with Joomla 2.5 and a plugin. The following code will create variables that tell you what component and view are currently being used:

$option = JRequest::getVar('option');
$view = JRequest::getVar('view');


If you're not looking at an article, return nothing so the plugin does nothing

When you know what option and view you're in, you should be able to use logic to return and change nothing if NOT in an article:

// if this page is not an article
if($option != "com_content" AND $view != "article")
return


I hope that helps! Keep us updated, hopefully we should be able to get this sorted out for you.
2013-11-18 3:32 pm
Thank you so much BradM, this works :) Now how was I supposed to find this on Joomla Docs ?
So getVar('view') says article on article page and returns category name on category page.
Staff
5,587 Points
2013-11-18 5:41 pm
Hello hassanz,

I'm glad Brad's code that he shared with you was able to work in your case, the reason I shared the Joomla development documentation is because it sounds like you're interested in learning to develop with Joomla, and without a deeper understanding of how things work within Joomla it can be difficult to get things to work the way you'd like.

The way that Brad instructed for you to do this should work, but it's actually already built-in to the onContentAfterTitle function with the $context variable as well if you just wanted to use that. This information was found in the Joomla Plugin Development pages for creating a content plugin.

If you re-did your helloworld.php script to simply be this:

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


It would have the exact same behavior, as it's checking first if the $context variable is com_content.category or a category page, and if it is it will return nothing. But if it doesn't detect that you're on a category page, it will go ahead and print out the Hello World! text.

Please let us know if you had any other questions.

- Jacob
2013-11-20 11:00 am
Thank You, that is some good explanation. Problem is with Joomla docs, its pretty concise rather than throwing more details. I'll keep you guys posted if I get questions. Thanks again.
Staff
5,587 Points
2013-11-20 12:11 pm
No problem hassanz, let us know if you run into any further snags.

- Jacob
n/a Points
2014-03-20 7:46 am

how can make simple module...???

 

Staff
4,058 Points
2014-03-20 8:31 am
Hello Vishit,

We do not have an article on makring a simple module, but there is a guide on creating a simple module in Joomla's documentation.
n/a Points
2014-03-27 12:23 am

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.

Staff
4,058 Points
2014-03-27 8:01 am
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 docs@inmotionhosting.com 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.
n/a Points
2014-03-27 12:37 pm

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!

Staff
5,587 Points
2014-03-27 12:53 pm
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

Post a Comment

Name:
Email Address:
Comment:
Are you a bot?
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!