underpin / rest-endpoint-loader
Rest endpoint loader for Underpin
1.1.0
2021-11-24 21:34 UTC
Requires
- underpin/underpin: ^2.0
This package is auto-updated.
Last update: 2024-11-25 04:06:04 UTC
README
Loader That assists with registering REST endpoints to a WordPress website.
Installation
Using Composer
composer require underpin/rest-endpoint-loader
Manually
This plugin uses a built-in autoloader, so as long as it is required before Underpin, it should work as-expected.
require_once(__DIR__ . '/underpin-rest-endpoints/rest-endpoints.php');
Setup
- Install Underpin. See Underpin Docs
- Register new rest endpoints as-needed.
Example
A very basic example could look something like this.
// Register rest endpoint underpin()->rest_endpoints()->add( 'example-endpoint', [ 'endpoint_callback' => function ( \WP_REST_Request $request ) { return [ 'result' => 'Wow this worked nicely!' ]; }, 'has_permission_callback' => '__return_true', 'rest_namespace' => 'example/v1', 'route' => 'example-endpoint', 'args' => [ 'methods' => 'GET' ], ] );
Alternatively, you can extend Rest_Endpoint
and reference the extended class directly, like so:
underpin()->rest_endpoints()->add('endpoint-key','Namespace\To\Class');
A common use-case is to extend Rest_Endpoint
with your own rest_namespace
to keep code DRY.