In our last few tutorials, we have been creating our own Joomla 2.5 plugin. The plugin is a simple Hello World plugin that prints Hello World before each article. We're going to make this plugin even better by allowing the user to specify custom text. If the user doesn't want to display "Hello World", they can display anything else they want, such as "Howdy Internet!"

In this Joomla 2.5 plugin tutorial, we're going to show you how to add a simple parameter to your plugin. Our parameter is going to be called "Alternative Text", and will allow the user to type in another message besides Hello World.

 

Step 1: Add the new parameter to your XML file

New parameters in Joomla 2.5 plugins are contained within fields and fieldsets, which are added to the plugin's xml file. We'll show you the basic code for adding a new parameter below, and in another tutorial we'll go into further details about how it works.

<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="content">
        <name>Hello World</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>This is my very first plugin! 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>
        <config>
                <fields name="params">
                        <fieldset name="basic">
                                <field name="alt-text"  type="text" default="" label="Alternative Text" description="Besides Hello World, you can specify other text here to print to the screen instead." />
                        </fieldset>
                </fields>
        </config>
</extension>

 

Step 2: Add a value to the parameter

alt-text-showing-in-plugin-settingsNow that that we have updated our xml file with a new attribute, we will give this attribute a new value. To do this, go to your Joomla 2.5 plugin manager and save a new value. Refer to the screenshot to the right to see where we entered our Alternative Text Howdy Internet!

 

Step 3: Accessing the Plugin Parameter from PHP

Now that we've saved our new plugin parameter, we can use it in our code! Accessing a plugin parameter can be done using the following code:

$this->params->get('attribute-name')

The following is our updated php code in helloworld.php:

<?php

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

class plgContentHelloworld extends JPlugin
{
        public function onContentAfterTitle($context, &$article, &$params, $limitstart)
        {
                // If the user has entered alternative text to use
                // besides "Hello World", then return that instead.
                if($this->params->get('alt-text'))
                        return $this->params->get('alt-text');
                else
                        return "<p>Hello World!</p>";
        }
}

?>

As you can see in the screenshot below, as we saved alternative text, it now shows instead of the standard Hello World text.
print-plugin-parameter

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?

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!