Loader That assists with registering REST endpoints to a WordPress website.
composer require underpin/rest-endpoint-loader
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');
- Install Underpin. See Underpin Docs
- Register new rest endpoints as-needed.
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.