Skip to main content

Build your first plugin

A small plugin can show the whole lifecycle. In this example, WordPress fires the login_message filter, a handler receives the existing markup, and an injected service supplies one line of copy.

Install the package​

Use a local WordPress installation with PHP 8.1–8.4 and Composer. From the WordPress root, create a plugin directory and install the stable v1 series.

mkdir -p wp-content/plugins/fieldnotes
cd wp-content/plugins/fieldnotes
composer require "x-wp/di:^1.10"

Add the plugin​

Create fieldnotes.php alongside the generated vendor/ directory. All three classes live in this file so the example needs no additional autoload configuration.

fieldnotes.php
<?php
/**
* Plugin Name: Fieldnotes
* Description: Adds a welcome message to the WordPress login screen.
*/

namespace Fieldnotes;

use XWP\DI\Decorators\Filter;
use XWP\DI\Decorators\Handler;
use XWP\DI\Decorators\Module;

defined( 'ABSPATH' ) || exit;
require __DIR__ . '/vendor/autoload.php';

final class Login_Copy {
public function welcome(): string {
return 'Welcome to Fieldnotes.';
}
}

#[Handler( tag: 'init', container: 'fieldnotes' )]
final class Login_Handler {
public function __construct( private Login_Copy $copy ) {}

#[Filter( tag: 'login_message' )]
public function welcome( string $message ): string {
return $message . '<p class="message">' . esc_html( $this->copy->welcome() ) . '</p>';
}
}

#[Module(
container: 'fieldnotes',
hook: 'plugins_loaded',
priority: 10,
handlers: array( Login_Handler::class ),
)]
final class Root_Module {}

xwp_load_app( array(
'id' => 'fieldnotes',
'module' => Root_Module::class,
'compile' => false,
) );

See the result​

Activate Fieldnotes under Plugins, then open /wp-login.php in a private browser window. Welcome to Fieldnotes. appears above the login form. You can also exercise the same WordPress filter from the WordPress root with WP-CLI.

wp plugin activate fieldnotes
wp eval 'echo apply_filters( "login_message", "" );'

xwp_load_app() schedules the build early on plugins_loaded. At priority 10 the root module registers Login_Handler. On init, the container constructs the handler, supplies its Login_Copy dependency by type, and registers the public filter method. Returning the original message plus the new paragraph preserves text from other plugins.

For an interface binding or a second module, continue to Modules and startup. For dependencies passed directly to a callback, see Handlers and hooks.