Not a developer? Go to MovableType.com

Documentation

Creating Your First Plugin

Your First Plugin

Your first plugin will have no functionality. It will merely contain the code necessary to declare itself and its associated meta data within the application. It will consist of only a single file.

  1. Create a folder called “TestPlugin” within the Movable Type plugins directory.
  2. Within the “TestPlugin” directory you just created, create a file called TestPlugin.pl with the following contents:

package MT::Plugin::TestPlugin;

use strict;
use base qw( MT::Plugin );
our $VERSION = '1.0'; 
my $plugin = MT::Plugin::TestPlugin->new({
   id          => 'TestPlugin',
   key         => 'test-plugin',
   name        => 'Test Plugin',
   description => "My first plugin",
   version     => $VERSION,
   author_name => "[your name]",
   author_link => "[URL to your homepage]",
   plugin_link => "[link to plugin's homepage]",
});
MT->add_plugin($plugin);
sub init_registry {
    my $plugin = shift;
    $plugin->registry({
    });
}

With that little bit of code you have successfully created your first plugin. Granted it doesn’t do anything, but in time we will evolve it to do much, much more.

The last 5 lines of the plugin you just created defines the components of the plugin that need to merged with Movable Type’s global registry. Through the course of the rest of this document and guide you will see many permutations of this subroutine. These code samples can easily be mixed, combined, re-combined and modified to progressively add more and more features to your plugin.

To see your plugin, login to Movable Type and from the System Overview pull down menu select “Plugins”. You should see your plugin listed there. If Movable Type was unable to load or process your plugin, then the plugin will automatically be disabled and MT will display the error message it received when trying to load the plugin below the plugins name on the plugin listing screen.

Back

5 Comments

Mark Carey

Mark Carey on August 8, 2007, 6:33 a.m. Reply

It would be good to add a description of the difference between name, id, and key, including character restrictions and best naming practices for each, and how each are used by MT4.

david

david on October 6, 2007, 5:50 p.m. Reply

Is there any way to debug this process? I’ve followed the above instructions but my plugin won’t load - and it won’t give me any more information beyond “Failed to load”. I’ve checked the Apache logs to no avail. Does MT4 have its own STDERR logs somewhere?

david

david on October 6, 2007, 6:01 p.m. Reply

False alarm - found the problem - but debugging info would be nice.

Jay Allen

Jay Allen on December 4, 2007, 1:05 p.m. Reply

Create a folder called “TestPlugin” within the Movable Type addons directory.

If you meant the “addons” directory instead of the “plugins” directory, that would be a massive change and should be called out more specifically. However, looking at the default distribution, I think you mean the “plugins” directory. Is this the case?

Jay Allen

Jay Allen on December 4, 2007, 1:21 p.m. Reply

If you write a lot of plugins, you may appreciate the following snippet which uses the last part of the package name for determining the ID, key and name (unless otherwise specified).

Also, “use MT 4.0” prevents the plugin from loading entirely if someone is using a previous version. This is a Very Nice Thing To Do(tm) to lessen confusion about why a plugin won’t run in an older environment.


package MT::Plugin::TestPlugin;

use strict;
use MT 4.0;
use base qw( MT::Plugin );

# Define $DISPLAY_NAME only if different from package ending (i.e. TestPlugin)
our $DISPLAY_NAME = ''; 
our $VERSION = '1.0'; 

our ($plugin, $PLUGIN_MODULE, $PLUGIN_KEY);
MT->add_plugin($plugin = __PACKAGE__->new({
   id          => plugin_module(),
   key         => plugin_key(),
   name        => plugin_name(),
   description => "My first plugin",
   version     => $VERSION,
   author_name => "[your name]",
   author_link => "[URL to your homepage]",
   plugin_link => "[link to plugin's homepage]",
}));
sub init_registry {
    my $plugin = shift;
    $plugin->registry({
    });
}

sub plugin_name     { return ($DISPLAY_NAME || plugin_module()) }
sub plugin_module   {
    $PLUGIN_MODULE or ($PLUGIN_MODULE = __PACKAGE__) =~ s/^MT::Plugin:://;
    return $PLUGIN_MODULE;
}
sub plugin_key      {
    $PLUGIN_KEY or ($PLUGIN_KEY = lc(plugin_module())) =~ s/\s+//g;
    return $PLUGIN_KEY
}

Leave a Comment