Tutorial: How to create a plugin?

Copyright by Jan Neugebauer
Created: April 20th 2007
Last edited: April 20th 2007

Used versions for this tutorial:
- CMSimple v2.8
- Plugin Loader version 2.0 beta 10 for CMSimple (>= v2.6)
- Example-Plugin version 1.2

Requirements:
- CMSimple (>= v2.6)
- Plugin Loader (>= v2.0 beta 9)
- Example Plugin
- »Download Plugin Loader / Example Plugin







How to create a plugin?

Basics steps:

1. Set plugin folder in CMSimples config.
2. Create a folder for your plugin in the plugins folder.
3. Create a file "index.php" and create some output.
4. Call index.php in content.htm to show the content on a CMSimple page.
5. Set up configuration files, that may be edited in admin area (plugin config, help, language, stylesheet).
6. Optional: Do more advanced plugin coding.







Table of contents:

1. Set plugin folder in CMSimples config
2. Create a folder for your plugin in the plugins folder
3. Create file "index.php"
4. Call plugin in CMSimple (content.htm)
5. Add the plugin to the admin area
6. Add main content for plugin in admin area
7. Create a plugin feature: function SayHello()
8. Plugin config, help, language and stylesheet


 
1. Set plugin folder in CMSimples config.
Set plugin folder in CMSimples config.

$cf['plugins']['folder']="plugins";


You my use any directory name you like, but why not name it "plugins"?
»previous step  -  »table of contents  -  »next step
 
2. Create a folder for your plugin in the plugins folder
Create a folder for your plugin:

• Use a unique name, other programers can not choose too easily.
• Avoid special characters.
• In this tutorial we name the folder "example_plugin".
»previous step  -  »table of contents  -  »next step
 
3. Create file "index.php"
Create file "index.php".

Inside "index.php" we generate some output.
We use function DoSomething() to return a string "Do something".
»previous step  -  »table of contents  -  »next step
 
4. Call plugin in CMSimple (content.htm) (I)
Now we call our plugin in a CMSimple page.

• Create a new page in CMSimple.
• Make sure the CMSimple Scripting part looks exactly as shown in the screenshot.

The page calls function DoSomething() from the "index.php" of the plugin. The page will show the output "Do Something" from the plugin function.

<H1>Example Plugin</H1>
#cmsimple $output.=DoSomething(); #

4. Browse page "Example Plugin" in CMSimple (II)
The page did call function DoSomething() from the "index.php" of the plugin via CMSimple Scripting. The page shows the output "Do Something" from the plugin function.
»previous step  -  »table of contents  -  »next step
 
5. Add the plugin to the admin area (I)
Now that we have a basic plugin functionality, we want to add some administration to the admin area.

If we log in to CMSimple now, we can't find the plugin. Read on ...

5. Add the plugin to the admin area (II)
Creating a file called "admin.php" in the plugin directory will add the plugin to the plugin select box in the admin area.

5. Add the plugin to the admin area (III)
The plugin was added to the plugin select box in the admin area.
»previous step  -  »table of contents  -  »next step
 
6. Add main content for plugin in admin area (I)


To print some output to the first page when the plugin is selected, we need to edit "admin.php".


Line 5, 7: Check, if the plugin was called (either by URI or form).
If so, continue doing some plugin stuff
(here: echo "Example Plugin").
Else skip this plugin.


Line 18, 19: To differentiate several plugin modes, we could use the argument "admin".


Line 26-28: If the plugin is called without any arguments, meaning $action is not set, we show some output ("Example Plugin").
I use this part to show Plugin copyright information. But you may also show main features of the plugin here.

6. Add main content for plugin in admin area (II)
Select "Example_plugin" from the plugin select box.

Below, the so called "Main Tab" is shown. By default language settings it is named "Plugin Main Settings".

In the content area we see our text "Example Plugin" which was added to the ouput of CMSimple ($o).
»previous step  -  »table of contents  -  »next step
 
7. Create a plugin feature: function SayHello() (I)
Next, we want to add some functionality to our plugin. Please edit index.php accordingly.

The feature is very basic. We create a function SayHello, which will output a name (given by argument or set to default).

7. Create a plugin feature: function SayHello() (II)
Edit content.htm accordingly.

<H1>Example Plugin</H1>
#cmsimple $output.=SayHello(); #

This is not needed for the admin area, but before we had the same page for calling function DoSomething() which no longer exists. To avoid errors we exchange the functions.

7. Create a plugin feature: function SayHello() (III)
Edit admin.php accordingly.


Line 26: This is still the same.


Line 29: If we call our plugin with argument "admin=plugin_main" function SayHello() is executed and ouputs some name to the content area.

The link in the Plugin Main Tab looks like
http://yourdomain.com/pathtocmsimple/index.php?&example_plugin&admin=plugin_main
This will call CMSimple ("index.php"), activating the Example Plugin ("?&example_plugin") in mode "&admin=plugin_main".


Line 38-43: For any other mode than "plugin_main" the Plugin Loader is called to either edit the config, help, language or stylesheet of the plugin.

7. Create a plugin feature: function SayHello() (IV)
If we now reload the Example Plugin, we can see the output generated by function SayHello().

»previous step  -  »table of contents  -  »next step
 
8. Plugin config, help, language and stylesheet
Noticed the new tabs
Plugin Stylesheet, Plugin Config, Plugin Language, Plugin Help ???

So, where do they come from?
The Plugin Loader is able to create them automatically, if you add the directories and files to the plugin directory.
It handles all editing of those files also.

Please see the files in the download of Example Plugin for the content of the files.
»previous step  -  »table of contents