Skip to main content

WordPress integrations

Each integration pairs a class attribute with a method attribute. To try one in the quickstart plugin, save the class in the indicated file. In fieldnotes.php, load that file after Composer's autoloader, for example with require_once __DIR__ . '/src/Greeting_Ajax.php';. Add the class to the handlers array on Root_Module alongside Login_Handler::class. All three examples use the quickstart's fieldnotes container ID and its plugins_loaded module hook. See module startup and handler registration and invocation.

Ajax_Handler and Ajax_Action​

WordPress dispatches requests to admin-ajax.php using the action request field. XWP-DI joins the prefix and action name with an underscore. This read-only action accepts a first name and returns a greeting to signed-in visitors and guests.

src/Greeting_Ajax.php
<?php
namespace Fieldnotes;

use XWP\DI\Decorators\Ajax_Action;
use XWP\DI\Decorators\Ajax_Handler;

#[Ajax_Handler( container: 'fieldnotes' )]
final class Greeting_Ajax {
#[Ajax_Action(
action: 'greet',
prefix: 'fieldnotes',
public: true,
method: Ajax_Action::AJAX_GET,
vars: array( 'name' => 'reader' ),
)]
public function greet( string $name ): void {
wp_send_json_success( array( 'greeting' => 'Hello, ' . $name . '!' ) );
}
}

Add Greeting_Ajax::class to the module. Open /wp-admin/admin-ajax.php?action=fieldnotes_greet&name=Ada under your WordPress installation. The response is {"success":true,"data":{"greeting":"Hello, Ada!"}}. Omitting name gives Hello, reader!.

AJAX_GET, AJAX_POST, and AJAX_REQ select where XWP-DI reads the declared variables. An action that needs to enforce an HTTP verb must check it explicitly. For ordinary fields, the callback receives vars values in declaration order, followed by any injected params. For a restricted action, set public: false to register only the signed-in hook, use cap for the required WordPress capability, and set nonce: 'security' to read a nonce from that field. For the prefix and action above, generate the nonce with wp_create_nonce( 'fieldnotes_greet' ). Ajax_Handler source · Ajax_Action source

REST_Handler and REST_Route​

Extend the global XWP_REST_Controller class. It connects the controller's initialization to route registration. The handler sets the namespace and base path. A route of '' serves the base path itself. WordPress passes the current WP_REST_Request to the callback.

src/Site_Controller.php
<?php
namespace Fieldnotes;

use XWP\DI\Decorators\REST_Handler;
use XWP\DI\Decorators\REST_Route;

#[REST_Handler( namespace: 'fieldnotes/v1', basename: 'site', container: 'fieldnotes' )]
final class Site_Controller extends \XWP_REST_Controller {
#[REST_Route( route: '', methods: 'GET', guard: '__return_true' )]
public function show( \WP_REST_Request $request ): \WP_REST_Response {
return new \WP_REST_Response( array( 'name' => get_bloginfo( 'name' ) ) );
}
}

Add Site_Controller::class to the module. On a site with pretty permalinks and the default REST prefix, open /wp-json/fieldnotes/v1/site. The JSON object's name field contains the site's title. In v1.10.0, REST context detection looks for the REST prefix followed by a slash in the request URI. A plain ?rest_route=/fieldnotes/v1/site URL can therefore skip this handler. Context detection

The route's guard becomes WordPress's permission_callback. Here __return_true makes the site title public. For a restricted route, use the name of a public controller method that checks the caller's capabilities. Pass WordPress route argument definitions through vars, or supply the name of a controller method that returns those definitions. REST_Handler source · REST_Route source · Controller base class

CLI_Handler and CLI_Command​

CLI_Handler registers a top-level WP-CLI command on cli_init. Each CLI_Command adds a subcommand. The args array defines the WP-CLI synopsis. XWP-DI binds positional arguments to PHP parameters by name and groups the declared options and flags into a parameter named $flags.

src/Greeting_CLI.php
<?php
namespace Fieldnotes;

use XWP\DI\Decorators\CLI_Command;
use XWP\DI\Decorators\CLI_Handler;

#[CLI_Handler( namespace: 'fieldnotes', container: 'fieldnotes' )]
final class Greeting_CLI {
#[CLI_Command(
command: 'greet',
summary: 'Print a greeting.',
args: array(
array( 'type' => 'positional', 'name' => 'name' ),
array( 'type' => 'flag', 'name' => 'upper', 'optional' => true ),
),
)]
public function greet( string $name, array $flags ): void {
$message = 'Hello, ' . $name . '!';
\WP_CLI::line( $flags['upper'] ? strtoupper( $message ) : $message );
}
}

Add Greeting_CLI::class to the module and run these commands from the WordPress installation with the plugin active.

wp fieldnotes greet Ada
# Hello, Ada!

wp fieldnotes greet Ada --upper
# HELLO, ADA!

WP-CLI generates usage information from the synopsis. Run wp help fieldnotes greet to see the positional argument and optional flag. CLI_Handler source · CLI_Command source