Skip to content

The Plugin Module

Miguel Muscat edited this page Nov 30, 2022 · 2 revisions

The plugin module is a special module that is automatically loaded into the plugin.

The purpose of this module is to expose services that provide information about the plugin, such as paths and metadata.

Paths

The plugin module provides the following services that relate to paths:

  • plugin/file_path - the path to the plugin's main file.
  • plugin/dir_path - the path to the plugin's root directory.
  • plugin/dir_url - the URL to the plugin's root directory.

Metadata

The plugin module provides the following services that relate to metadata:

  • plugin/meta - a PluginMeta object containing all the plugin's metadata.
  • plugin/slug - the plugin's slug.
  • plugin/version - the plugin's version.
  • plugin/code - the plugin's ID-like abbreviated code name.
  • plugin/name - the plugin's name.
  • plugin/description - the plugin's description.
  • plugin/author - an [AuthorMeta] object containing metadata for the plugin's author.
  • plugin/textDomain - the plugin's text domain.
  • plugin/domainPath - the path to the plugin's languages folder.
  • plugin/minPhpVersion - the minimum PHP version required to run the plugin.
  • plugin/minWpVersion - the minimum WordPress version required by the plugin.
  • plugin/extra - any extra data specified in the plugin.json file, as an associative array.

This metadata is read from the plugin's plugin.json file, if it exists. If not, it is parsed from the plugin's main file header, using the WordPress' get_plugin_data() function.

Activation & Deactivation Hooks

The plugin module registers hooks for both plugin activation and deactivation events, and triggers normal WordPress actions for these events. This allows your plugin to use Module::getHooks() to listen for these events, reducing the need for imperative code in your modules' run() method.

class MyModule extends Module
{
    public function getHooks(): array
    {
        return [
            'example/early_activation' => [
                new Handler([], function () { /*... */ })
            ],
            'example/activation' => [
                new Handler([], function () { /*... */ })
            ],
            'example/deactivation' => [
                new Handler([], function () { /*... */ })
            ],
        ];
    }
}

The actions triggered by the plugin module are prefixed with the code specified by your plugin's metadata. In the above example, this can be seen as the example/ prefix. The / delimiter is the same as the one used to delimit your module's scoped services.

The */early_activation action is triggered immediately on activation. Hooking into this action is equivalent to using the register_activation_hook() function.

The */activation action is triggered after WordPress performs its redirect after a plugin has been activated. For more information about this redirect, check out the WordPress Codex page on plugin activation. Hooking into this action is equivalent to using a transient to detect the activation later. By default, the SDK uses a 60-second transient. You can extend the plugin/activation_marker service to change this. Normal options are also supported.

The */deactivation action is triggered immediately on deactivation. Hooking into this action is equivalent to using the register_deactivation_hook() function.

Clone this wiki locally