| name | rest-endpoint |
| description | Create a new REST API endpoint controller for the Saman SEO plugin. Use when adding new API endpoints, creating backend functionality for React admin pages, or building AJAX-like functionality. |
Create REST API Endpoint
Generate a new REST API controller following the Saman SEO plugin patterns.
Arguments
$ARGUMENTS should contain: endpoint name (e.g., "keywords") and description
Steps
-
Analyze the request to determine:
- Endpoint name and route (e.g.,
/saman-seo/v1/keywords)
- HTTP methods needed (GET, POST, PUT, DELETE)
- Request/response schema
- Permission requirements
-
Create the controller at includes/Api/class-{name}-controller.php
-
Follow this template:
<?php
namespace Saman\SEO\Api;
use WP_REST_Controller;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class {Name}_Controller extends WP_REST_Controller {
protected $namespace = 'saman-seo/v1';
protected $rest_base = '{endpoint-base}';
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'id' => array(
'description' => __( 'Unique identifier.', 'saman-seo' ),
'type' => 'integer',
),
),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
public function get_items_permissions_check( $request ) {
return current_user_can( 'manage_options' );
}
public function get_items( $request ) {
$items = array();
return new WP_REST_Response( $items, 200 );
}
public function create_item_permissions_check( $request ) {
return current_user_can( 'manage_options' );
}
public function create_item( $request ) {
$params = $request->get_json_params();
return new WP_REST_Response( array( 'success' => true ), 201 );
}
public function get_item_schema() {
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => '{name}',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier.', 'saman-seo' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
}
}
-
Register the controller in includes/class-saman-seo-plugin.php:
- Add to the
boot() method's API controller registration
- Example:
$controller = new \Saman\SEO\Api\{Name}_Controller();
$controller->register_routes();
-
Add to autoloader if using non-standard naming
Patterns to Follow
- Namespace:
namespace Saman\SEO\Api;
- Extend: Always extend
WP_REST_Controller
- Route Prefix:
/saman-seo/v1/
- Permissions: Use
current_user_can( 'manage_options' ) for admin endpoints
- Sanitization: Sanitize ALL input data
- Response: Return
WP_REST_Response or WP_Error
- Schema: Define item schema for documentation
Example Usage
/rest-endpoint keywords Manage focus keyphrases and keyword analysis