Skip to content
Dimitri BOUTEILLE edited this page Feb 21, 2025 · 5 revisions

Welcome to the wp-module-rest-api wiki!

This module designed for developers that want to add routes to the WordPress Rest API in a few moments.

Documentation

Installation

Requirements

The server requirements are basically the same as for WordPress with the addition of a few ones :

Installation with composer

You can use Composer. Follow the installation instructions if you do not already have composer installed.

composer require dbout/wp-module-rest-api

In your PHP script, make sure you include the autoloader:

require __DIR__ . '/vendor/autoload.php';

Usage

Before creating your first route, you must initialize the module. It is advisable to add this code at the beginning of the functions.php file of your theme or in a mu-plugin.

use Dbout\WpRestApi\RouteLoader;

// One folder
$loader = new RouteLoader(__DIR__ . '/src/Api/Routes');

// Multiple folders
$loader = new RouteLoader([
    __DIR__ . '/themes/my-theme/api'
    __DIR__ . '/src/Api/Routes',
]);

// You can also use pattern
$loader = new RouteLoader(__DIR__ . '/src/Modules/*/Api/Routes');

$loader->register();

Note

The module will automatically search for all classes that are in the folder and sub folder.

Note

You can pass as the second argument of RouteLoader an option object: read the documentation.

Now you have initialized the module, you just need to create your first route in the routes folder.

<?php

use Dbout\WpRestApi\Attributes\Route;
use Dbout\WpRestApi\Attributes\Action;

#[Route(
    namespace: 'app/v2',
    route: 'document/(?P<documentId>\d+)'
)]
class Document 
{

    #[Action(Method::GET)]
    public function get(\WP_REST_Request $request): \WP_REST_Response
    {
        // Add your logic 
        $id = $request->get_param('documentId');
       
        return new \WP_REST_Response([
            'success' => true,
        ]);
    }

   #[Action(Method::DELETE)]
    public function delete(\WP_REST_Request $request): \WP_REST_Response
    {
        // Add your logic 
        $id = $request->get_param('documentId');
        
        return new \WP_REST_Response([
            'success' => true,
        ]);
    }
}

You just created 2 routes ๐ŸŽ‰

  • GET:wp-json/app/v2/document/18
  • DELETE:wp-json/app/v2/document/18

The logic is extremely simple, you can use the following methods: GET, POST, PUT, PATCH and DELETE

If you need, you can define multiple methods for an action by passing a method array :

#[Action([Method::GET, Method::POST, Method::PUT])]
public function execute(\WP_REST_Request $request): \WP_REST_Response
{
    // Add your logic 

}

Callback arguments

If your route contains parameters, you can retrieve them as an argument for your function :

<?php

use Dbout\WpRestApi\Attributes\Route;
use Dbout\WpRestApi\Attributes\Action;

#[Route(
    'app/v2',
    'document/(?P<documentId>\d+)'
)]
class Document
{

    #[Action(Method::GET)]
    public function get(int $documentId): \WP_REST_Response
    {
        // Add your logic

        return new \WP_REST_Response([
            'success' => true,
        ]);
    }
}

Note

If your function contains a WP_REST_Request argument, the WP_REST_Request object will be passed as an argument.

Contributing

๐Ÿ’• ๐Ÿฆ„ Contributions are most welcome so feel free to reach out, post issues and / or propose improvements.

Clone this wiki locally