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:

This basic plugin attempts to do the following:

  • 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:setting
        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>
Here is what that configuration template results in:

ppdb_settings.png As you can see when you click on the link, it creates three new entries under "Settings" under the plug-in's drop down information box. To the left you have a label for the text field, which explains the gist of what the input type does. You then have the large text area which is the input for this, and it is named "btn_html" which is the variable that the HTML for the PayPal donate button is stored under in the database. Then you have a "hint" below the text field.

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:

Leave a comment

March 2010

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Recent Entries

A window into the totalitarian mind of the left on freedom of religion
From Digg: Me: I'm not going to hold my breath waiting for the same liberal democrats who shriek about the…
Google's lossy compiler
Google's closure compiler service gets a little too frisky under ADVANCED_OPTIMIZATIONS. Original code: With advanced optimizations enabled, it was able…
The three purposes of the federal income tax law
Businesses will spend about 3.4 billion man-hours and individuals about 1.7 billion hours figuring out their taxes this year.…

Subscribe

Advertisements

OpenID accepted here Learn more about OpenID