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.
- Create a folder called “TestPlugin” within the Movable Type
plugins
directory. - 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.
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 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 on October 6, 2007, 6:01 p.m. Reply
False alarm - found the problem - but debugging info would be nice.
Jay Allen on December 4, 2007, 1:05 p.m. Reply
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 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.