cuberis/wp-rest-endpoints
Composer 安装命令:
composer require cuberis/wp-rest-endpoints
包简介
Add custom Rest API endpoints to your WordPress site.
README 文档
README
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:
composer require cuberis/wp-rest-endpoints
- 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
function my_register_rest_routes() { new Cuberis\REST_API\Post_Type_Controller('my_post_type_slug', [ 'namespace' => 'cuberis', 'version' => 1, 'endpoint' => 'my-post-type' ]); } add_action( 'rest_api_init', 'my_register_rest_routes' );
Extend Base_Controller for a Custom Endpoint
https://my-website.com/wp-json/cuberis/v1/whatever
class My_Custom_Controller extends Cuberis\REST_API\Base_Controller { public function __construct() { parent::__construct([ 'namespace' => 'cuberis', 'version' => 1, 'endpoint' => 'whatever' ]); } public function get_items( $request ) { return rest_ensure_response(['Testing']); } } function my_register_rest_routes() { new My_Custom_Controller(); } add_action( 'rest_api_init', 'my_register_rest_routes' );
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:
themes/your-theme-name/ ├── templates/ │ ├── partials/ │ │ ├── partial-my_post_type.php
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": 1234,
"title": "My Post Title",
"html": "<article>\n<h2>\nMy Post Title\n</h2>\n</article>\n"
}
]
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:
new WP_Query([
'post_type' => 'my-post-type',
'posts_per_page' => 4,
'paged' => 2,
'tax_query' => [
[
'taxonomy' => 'my_category',
'field' => 'slug',
'terms' => [
'slug1',
'slug2'
]
]
]
])
Note: Taxonomy queries via endpoints currently only supports the slug field type.
Optional Filters
cuberis_rest_cpt_result
function cuberis_filter_api_result( $result, $post_id ) { return [ 'pageID' => $post_id, 'testing' => 'yep!' ]; } add_filter('cuberis_rest_cpt_result', 'cuberis_filter_api_result', 10, 2);
cuberis_rest_api_query_args
function cuberis_filter_api_args( $args, $params ) { $args['order'] = 'ASC'; return $args; } add_filter('cuberis_rest_api_query_args', 'cuberis_filter_api_args', 10, 2);
cuberis/wp-rest-endpoints 适用场景与选型建议
cuberis/wp-rest-endpoints 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cuberis/wp-rest-endpoints 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cuberis/wp-rest-endpoints 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2019-03-13