Skip to content

Making Your First Module

Daniel Pimley edited this page Aug 3, 2018 · 18 revisions

Modules are the most powerful and general-purpose of the four types of extensions, but a module does not have to be complicated for it to be useful. This article will teach you how to make a very simple module that greats the user with a message when they log in.

The Basics

The bare minimum required to make a functioning module is this: a sub-directory in your installation's modules directory containing a PHP file with a matching name; for this example, we will create a directory named my_awesome_module that contains a file named my_awesome_module.php. The contents of the PHP file will be as follows:

<?php
    class MyAwesomeModule extends Modules {
        public function user_logged_in() {
            Flash::notice(__("Hello!", "my_awesome_module"));
        }
    }

The most important thing to note in this file is that it extends the Modules class with a class named after the file and its containing directory (using camelization rules). The class has only one public function; this is a trigger responder that will be called when the user logs in and the call "user_logged_in" is triggered. The function calls Chyrp Lite's Flash class and tells it to display a notice to the user; the notice is defined using the __() helper that will translate the message into the blog's chosen language if our module has a suitable translation file installed.

That's all we need to make the most basic module, but we can add some niceties.

The Niceties

We can add a file named info.php to our module's directory. This is a PHP file that contains metadata about our module, some of which will be displayed in the admin console. The contents are as follows:

<?php
return array(
    "name"          => __("My Awesome Module", "my_awesome_module"),
    "url"           => "http://example.com/",
    "version"       => 1.0,
    "description"   => __("Greetings, users!", "my_awesome_module"),
    "author"        => array(
        "name"      => "Anonymous Coward",
        "url"       => "http://myhomepage.com/")
);

This file is particularly useful if you plan to distribute your module for others to use. The other thing that can be useful in this case is a .pot translation template file. To generate one of these, first you need to create the directory structure locale/en_US/LC_MESSAGES/ in your module's directory. Once your module has been added to your Chyrp Lite installation, you can visit the URL [your domain]/[chyrp path]/tools/gettext.php to scan the installation and create .pot files for all translated strings. Once this is done you will find a new file at modules/my_awesome_module/locale/en_US/LC_MESSAGES/my_awesome_module.pot that can be used to create translation files.

Clone this wiki locally