Zealousweb
ExpressionEngine Plugin

A Complete Guide On Creating An ExpressionEngine Plugin

August 24, 2021Posted By: ZealousWeb
Plugin DevelopmentWeb Development TutorialWebsite Plugin Creation

Introduction

To supercharge your ExpressionEngine website’s performance, harness the might of robust plugins. Staying in step with the latest trends, plugins are your trusty companions. In this article, I’m here to guide you through the gentle journey of crafting a basic ExpressionEngine plugin.

Whether you’re an aspiring developer or an experienced hand, this step-by-step process ensures you’re on the right path. Unleash the potential of your site and offer your users a seamless experience. So, let’s roll up our sleeves and delve into the creative world of plugin development for ExpressionEngine, where endless possibilities await.

Name Your Plugin

The initial and crucial step in creating a website plugin is naming it.

Here’s a detailed example of naming your ExpressionEngine plugin: Ensure the name can be converted into a short, lowercase alphanumeric text with spaces replaced by underscores.

For instance, if you wish to name your plugin “Zeal World,” you can easily create a concise name in lowercase alphanumeric format by replacing spaces with underscores. In this way, “Zeal World” transforms into “zeal_world.”

Here’s An Illustrative Example:

  • Plugin file name (e.g., pi.zeal_world.php)
  • Plugin folder name (e.g., zeal_world)
  • Plugin template tag (e.g., {exp:zeal_world})

Create Plugin File Structure

ExpressionEngine Plugin

Starting from version 3.0, every ExpressionEngine add-on should include an “addon.setup.php” file within its package directory. This setup file contains essential details about the add-on, including information about the author, name, and version.

To begin, you should establish a folder for the add-on, for instance, “zeal_world.” This folder will eventually house the “addon.setup.php” file.

<?php return array( 'name' => 'Zeal World', 'description' => 'Our first plugin', 'author' => 'ZealousWeb', 'author_url' => 'https://zealousweb.com/', 'version' => '1.0.0', 'namespace' => 'ZealousWeb\ZealWorld', );

Certainly, here’s a rephrased explanation of the provided information:

  • Addon Name: This represents the title of the addon.
  • Plugin Description: This is a brief explanation of what the plugin does, and it’s a mandatory requirement for all plugins.
  • Author and Author URL: These indicate the name and website link of the individual or company responsible for the addon.
  • Addon Version: This denotes the specific version of the addon.
  • Namespace: This is a unique identifier for your addon within EE, ensuring that your code doesn’t clash with other addons. It’s usually a combination of your name or company and the addon name, without spaces and with capital letters for separate words.

Displaying Only Text Plugin

Generate a plugin file named ‘pi.zeal_world.php’ that exclusively displays text.

<?php class Zeal_world { public $return_data; public function __construct() { $this->return_data = 'Zeal World'; } } // End of file pi.zeal_world.php ?>

The plugin returns template text by retrieving a value from the class within the constructor method.

Plugin Featuring Multiple Functions

A plugin file can contain an unlimited number of functions, allowing us to include multiple functions with additional segments within the tag. For instance, the {exp:hello_zeal} tag can be extended to {exp:hello_zeal:show}, with the third segment specifying the tag’s function.

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

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

Please note that the $return_data variable is no longer required when utilizing the ‘show’ method. Instead of assigning the value to $return_data, we can directly return the desired output. The use of the $return_data variable is only necessary due to the limitation that PHP constructors cannot return values.

Additional functions can be incorporated into the plugin by introducing new methods within 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 ExpressionEngine Plugin; which we usually use 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 using tag 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

We have completed the process, and you can now install it from the add-ons section. Once installed, simply insert the {exp:zeal_world} tag into your template, and it will display ‘Zeal World’ in its place. Similarly, for multiple functions, you can use the {exp:zeal_world:show name=’Zeal’} tag in your template.

The ExpressionEngine for CMS is steadily gaining recognition as a prominent platform. As ExpressionEngine developers, it’s crucial to master creating add-ons to reap the rewards of your hard work. ZealousWeb’s ExpressionEngine development team comprises curious and innovative developers eager to explore this domain. Feel free to reach out to us to learn more about this topic.

FAQ

What Are The Basic Steps To Start Developing An ExpressionEngine Plugin?

How Can You Ensure Compatibility And Security In Your ExpressionEngine Plugin?

How To Optimize ExpressEngine Plugin With Advanced Techniques And Tips?

Table of Contents

Subscribe To Our Newsletter

Related Blog Posts