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:

Continued Education in Course 215: Joomla 2.5 Content Plugin Development
You are viewing Section 1: How to create a Hello World content plugin for Joomla 2.5
Section 2: How to change the name of a Joomla 2.5 Plugin
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 this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

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
8,910 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
17,172 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
9,968 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
9,968 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
9,260 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
9,968 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
5,399 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
9,968 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
9,968 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
8,910 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
8,910 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
9,968 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
n/a Points
2014-05-28 7:05 am

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

I  followed all the steps 

Please Help

Staff
8,910 Points
2014-05-28 7:59 am
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.
n/a Points
2014-07-30 6:20 am

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.

Staff
8,910 Points
2014-07-30 9:12 am
Could you clarify what other plugins you have currently active on your Joomla site?
n/a Points
2014-07-30 9:28 am

For the wordpress site 

Staff
9,260 Points
2014-07-30 9:49 am
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
Staff
5,399 Points
2014-07-30 9:44 am
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.
n/a Points
2014-08-27 3:43 am

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 accessdefined('_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>

 

Staff
19,241 Points
2014-08-27 10:50 am
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

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

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

34 Questions & Comments

Post a comment

Back to first comment | top

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!