If you want to enhance the performance of your ExpressionEngine website, you need to use powerful plugins. Relying on them is one of the best ways of keeping up with the trends. In this post, I will walk you through a step-by-step process of creating a simple ExpressionEngine plugin.

Name Your Plugin

While creating a plugin for your website, the first and most vital step that you have to take is, give it a name.

Let me give you a detailed example of how to name your plugin. Remember, you should be able to generate a short name in the lowercase alphanumeric text with underscores replacing spaces from the name that you want to use.

Let’s say, you want to name your plugin Zeal World. Now you can quickly generate a short name in lowercase alphanumeric text with an underscore replacing the space. If you do that, Zeal World becomes zeal_world.

Here is a detailed example:

  • Name of plugin file ( e.g: pi.zeal_world.php)
  • Name of plugin folder ( e.g: zeal_world )
  • Plugin template tag ( e.g: {exp:zeal_world})

Create Plugin’s File Structure

After version 3.0, each add-on in ExpressionEngine must have an addon.setup.php file in its package directory.

Addon setup file gives descriptive data about a particular add-on, such as author, name, and version.

First, you need to create a folder, for a plugin, e.g, zeal_world, which will eventually create an addon.setup.php file.

<?php

return array(
‘name’ => ‘Zeal World’,
‘description’ => ‘Our first plugin’,
‘author’ => ‘ZealousWeb,
‘author_url’ => ‘http://zwtdev.siteproofs.net/’,
‘version’ => ‘1.0.0’,
‘namespace’ => ‘ZealousWeb\ZealWorld’,
);

Here Is What It Means

name: This is the name of the addon

description: This is a description of the plugin, and all the plugins require it.

author and author_url: This is the name/URL of the company, or individual responsible for the add-on

version: This is a version of the addons.

namespace: This is a unique value to your add-on that EE uses to prevent any of your code from getting confused with other add-ons. Typically, it is a combination of your name or company and the add-on name without spaces and with words capitalized.

Displaying Only Text Plugin

Create plugin file with this name pi.zeal_world.php

<?php 

class Zeal_world {

    public $return_data;

    public function __construct()

    {

        $this->return_data = ‘Zeal World’;

    }

}

// End of file pi.zeal_world.php

The plugin outputs the template text by returning the value from the class to return a value from the constructor method.

Plugin With Multiple Functions

A plugin file can have any number of functions; we can add more than one function with extra segments to the tag. For example, e.x {exp:hello_zeal} tag could become {exp:hello_zeal:show}.
The 3rd segment describes the tag’s function.

\
Adding a new tag to the plugin is as simple as adding a new method to the plugin’s class that corresponds to the tag 3’rd segment.

public function show() {
return “Zeal, World”;
}

Note: We no longer use the $return_data variable when using the show method. We can return the value that we want to display rather than assign it to the $return_data. We use the $return_data variable only because PHP constructors cannot return a value.

We could add more functions to the plugin by adding new methods to the plugin’s class.

There are two different ways of fetching data into the plugin:

Parameters

Parameters allow you to pass small bits of information to the plugin; we usually used to configure how the plugin works.

For e.g {exp:zeal_world:show name=”Zeal”}

Here’s how we can access data from the plugin:

public function show() {
$name = ee()->TMPL->fetch_param(‘name’, ‘World’);
return ‘Hello, ‘ . $name . ”;
}

The output of the above tag: ‘Hello Zeal’

The fetch_param method ( EE template class); it works as shown below:

  • The first parameter “name” as the first parameter
  • The second is the (optional) default value for when the tag parameter is not set in the tag.
    In this case, if the name= parameter is missing from the template tag then the default value of “World” will be used.

Using Tag Data

Tag data is made by using tags pairs in the template.

For e.g
{exp:zeal_world:show}Create EE basic plugin{/exp:zeal_world:show}

When we are using a tag pair, the contents between the opening and closing tags can be used by the plugins.

public function show() {
$tagdata = ee()->TMPL->tagdata;
return ‘Hello, ‘ . $tagdata . ”; }

The output of the above tag: ‘Hello, Create EE basic plugin’

In this case, we get the data using the ee()->TMPL->tagdata property.

Conclusion

Finally, we are done; now you can install it from the add-ons section. After installing, include the tag {exp:zeal_world} in a template; it will display “Zeal World” in its place, in the same way as multiple functions include the tag {exp:zeal_world: show name=”Zeal”} in a template.

ExpressionEngine CMS is slowly working its way up to being another prominent platform in the group. As ExpressionEngine developers, you must learn to create the add-ons to enjoy the fruits of your hard work. ZealousWeb’s ExpressionEngine development team is a combination of inquisitive and innovative developers who want to experiment within the arena. Get in touch with us to find out more about this topic!