Example Plugin

A Typemill plugin is essentially a collection of files that add functionality or extend the system in some way.

Marie Curie

In this article, we'll walk through the creation of a simple plugin for Typemill, focusing on how to add custom functionality without modifying the core CMS. This example will help you understand how to create and structure a plugin, register it with Typemill, and make use of Typemill's powerful Plugin Event System.


Creating the Plugin

A Typemill plugin is essentially a collection of files that add functionality or extend the system in some way. To start, we need to create a folder for the plugin and include a main PHP file that will handle the plugin's functionality. Inside the example-plugin.php file, you'll register the plugin and define its behavior. Here's an outline of what the file should look like:

<?php
/*
Plugin Name: Example Plugin
Description: A simple plugin that listens for user registration and sends a welcome message.
Version: 1.0
Author: Your Name
*/

// Registering the plugin with Typemill
add_action('plugin_loaded', function() {
    // Register event listeners
    add_action('user_registered', 'send_welcome_message');
});

// Defining the event listener function
function send_welcome_message($user) {
    // Custom code to send a welcome message
    echo "Welcome, " . $user['name'] . "!";
}

Explanation of the Code

Plugin Header

The top part of the example-plugin.php file includes metadata about the plugin. This is important for Typemill to recognize and display the plugin in the admin interface. Key pieces of information include:

  • Plugin Name: The name of the plugin.
  • Description: A brief overview of what the plugin does.
  • Version: The version number of the plugin.
  • Author: The name of the plugin author.

Registering the Plugin

The add_action function registers our plugin with Typemill’s core system. The plugin_loaded action tells Typemill when to load the plugin. This ensures that the plugin is initialized when the system is ready to process it.

add_action('plugin_loaded', function() {
 add_action('user_registered', 'send_welcome_message');
});

This code registers the plugin's event listener for the user_registered event, which is triggered whenever a new user registers in Typemill.

Defining the Event Listener

Next, we define the function send_welcome_message, which will be executed when the user_registered event is fired. This function receives the $user object as a parameter, which contains information about the newly registered user.

In this example, we simply display a message welcoming the user. In a real-world plugin, this could be replaced with more complex actions, such as sending an email or updating a database.

function send_welcome_message($user) {
 echo "Welcome, " . $user['name'] . "!";
}

Testing the Plugin

To test your plugin, place the example-plugin.php file in the plugins directory of your Typemill installation. Then, navigate to the Plugins section in the Typemill admin interface and activate the plugin. Once activated, the plugin will begin listening for the user_registered event.

Try registering a new user, and you should see the welcome message displayed as a result of the event listener.


Conclusion

This simple example demonstrates how to create a plugin for Typemill, register event listeners, and define custom functionality in response to system events. By using the Plugin Event System, you can extend Typemill without modifying the core code, ensuring that your customizations remain upgradable and maintainable. This approach is powerful for building highly customized workflows, notifications, and integrations with third-party services.