A better "Hello World" Movable Type plugin
Before you look at this sample code, you need to read the following existing tutorials provided by the Movable Type team:
- Define the basic information about the plugin for the plugin manager.
- Registers a function template tag that will display the contents of the HTML associated with the PayPal donate button.
- Provides a configuration template, to allow you to input the PayPal donate button's HTML in the plugin manager.
Alright, without further ado, here's the full code:
package MT::Plugin::PayPalButtonPlugin;
use strict;
use base qw(MT::Plugin);
##For a full list of these values, check <a href="http://www.movabletype.org/documentation/man/MT/Plugin.html#ARGUMENTS">the documentation</a> for MT::Plugin
my $plugin = MT::Plugin::PayPalButtonPlugin->new({
key => 'mt-paypalbtn',
name => 'MT PayPal Button Plugin',
description => 'Provides a template tag that will insert the code for your PayPal donate button.',
author => 'Moi, non vouz',
author_link => 'http://www.codemonkeyramblings.com',
settings => new MT::PluginSettings([
['btn_html', {Default => "", Scope => 'blog'}]
]),
config_template => 'config.tmpl'
});
MT->add_plugin($plugin);
sub init_registry
{
my $plugin = shift;
$plugin->registry({
tags => {
function => {
'PayPalButton' => \&payPalButtonInclude
}
}
});
}
sub payPalButtonInclude
{
my $ctx = shift;
my $blogId = $ctx->stash("blog_id");
my $retVal = $plugin->get_config_value("btn_html", "blog:$blogId");
return $retVal;
}
Now, here is the markup code for the config.tmpl file:
<mtapp:settingHere is what that configuration template results in:
id="paypal_text"
label="PayPal Button HTML"
hint="<__trans phrase="Put the HTML for your PayPal Button here.">"
show_hint="1"
>
<textarea name="btn_html" id="btn_html" cols="70" rows="15"><mt:if name="btn_html"><$mt:var name="btn_html"$></mt:if></textarea>
</mtapp:setting>
When you hit the save changes button, it will take the contents of all <mtapp:settings> fields and store them for the plugin in the database. All a <mtapp:settings> really is, is a Movable Type way of putting some extra context and formatting information around a form field. You can add your own formatting HTML to these forms to lay out the settings inputs as you may see fit, and all of the regular HTML form inputs are obviously supported.
The proper format for the data that goes into the plugin description (The line that defines $plugin) is a two-dimensional list of data where the inner list of data is a string that gives a name to the setting option, followed by a hash of information about the setting. Default and Scope are two good values to always include. Default's value varies depending on the type of the field. Scope is either "blog" or "system."
This is what the directory structure should look like:
- MT/
- ----->plugins/
- ------>--------->PayPalButton/
- ------>--------->------------------------>PayPalButton.pl
- ------>--------->------------------------>tmpl/
- ------>--------->------------------------>----->config.tmpl
Additional reading:

