Auto-enabling Textpattern Plugins
Categories: Code, Projects
Posted:
Upload plugin. View the code and help. Scroll. Click “submit”. Scroll. Click “No” to enable plugin. I’ve repeated this pattern way to often whenever I set up or update a Textpattern based site. No more.
Textpattern added the ability for plugins to run some code when they are installed; PLUGIN_LIFECYCLE_NOTIFY. This was intended to let a plugin update database schemas, create default forms, or do any of the other set up work the plugin needs to only do once.
Going forward, I plan on updating all of my plugins to enable themselves when installed. My first public plugin to gain this benefit is mem_self_register v0.9.9.
Release Notes:
- added ability to customize new password reset mail message.
- auto-enabled on install.
- numerous fixes related to creating email messages.
How to auto-enable a plugin
Set the plugin[‘flag’].
$plugin['flags'] = PLUGIN_LIFECYCLE_NOTIFY;
Register the callback function.
if (@txpinterface == 'admin')
{
register_callback('mem_self_auto_enable', 'plugin_lifecycle.mem_self_register', 'installed');
}
Provide the function that sets the status of the plugin in the database.
/** Automatically enable plugin when installed */
function mem_self_auto_enable($event, $step)
{
$plugin = substr($event, strlen('plugin_lifecycle.'));
$prefix = 'mem_self_register';
if (strncmp($plugin, $prefix, strlen($prefix)) == 0)
{
safe_update('txp_plugin', "status = 1", "name = '" . doSlash($plugin) . "'");
}
}