Download the PHP package cuberis/wp-rest-endpoints without Composer
On this page you can find all versions of the php package cuberis/wp-rest-endpoints. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cuberis/wp-rest-endpoints
More information about cuberis/wp-rest-endpoints
Files in cuberis/wp-rest-endpoints
Package wp-rest-endpoints
Short Description Add custom Rest API endpoints to your WordPress site.
License GPL-2.0-or-later
Informations about the package wp-rest-endpoints
REST API Endpoints for WordPress
Add custom REST API endpoints to your WordPress site. This package provides a basic system for adding simple endpoints to the WP REST API.
Requirements
- PHP 5.6+
- WordPress 4.7+
Installing
-
Install via composer:
- Add your desired endpoint(s)
- Create your individual result templates for the REST responses (if necessary)
Adding Endpoints
Note: when adding new endpoints, make sure to hook into rest_api_init
.
Add Route for Custom Post Type
https://my-website.com/wp-json/cuberis/v1/my-post-type
Extend Base_Controller
for a Custom Endpoint
https://my-website.com/wp-json/cuberis/v1/whatever
Templates
Post Type Endpoints
If you are using the default reponse from your post type endpoints, each endpoint will need a template. Templates are PHP files that live in the templates/partials/
directory within your theme. Each template should be named: partial-{post-type-slug}
. Example:
If you register multiple post types for an endpoint, the library will look for a partial named partial-all.php
.
In your template file, the only thing you will have available from the post will be the ID in the form of an $id
variable.
Custom Endpoints
When defining custom endpoints, you have complete control over your API response so you can define if or how you use templates.
About the endpoints
By default, endpoints will return the following JSON format:
id
is the raw post ID, title
is your post title returned from get_the_title()
and html
is the raw HTML returned from your template file.
Using URL Parameters
Endpoints are powered by URL parameters which should match up almost identically with WP_Query vars. So for example, you could use:
The API would then return posts from the following WP_Query:
Note: Taxonomy queries via endpoints currently only supports the slug
field type.