airesvsg/acf-to-rest-api
Composer 安装命令:
composer require airesvsg/acf-to-rest-api
包简介
Exposes Advanced Custom Fields Endpoints in the WordPress REST API
README 文档
README
Exposes Advanced Custom Fields Endpoints in the WordPress REST API
https://wordpress.org/plugins/acf-to-rest-api/
- Installation
- Endpoints
- Filters
- Deprecated Filters 🆕
- Request API Version 🆕
- Field Settings 🆕
- Editing the Fields
- Examples
- Get ACF Fields Recursively 🆕
- Cache
Installation
- Copy the
acf-to-rest-apifolder into yourwp-content/pluginsfolder - Activate the
ACF to REST APIplugin via the plugin admin page
Endpoints
| Endpoint | READABLE | EDITABLE |
|---|---|---|
| /wp-json/acf/v3/posts 🆕 | ✅ | ❌ |
| /wp-json/acf/v3/posts/{id} | ✅ | ✅ |
| /wp-json/acf/v3/posts/{id}/{field-name} | ✅ | ✅ |
| /wp-json/acf/v3/pages 🆕 | ✅ | ❌ |
| /wp-json/acf/v3/pages/{id} | ✅ | ✅ |
| /wp-json/acf/v3/pages/{id}/{field-name} | ✅ | ✅ |
| /wp-json/acf/v3/users 🆕 | ✅ | ❌ |
| /wp-json/acf/v3/users/{id} | ✅ | ✅ |
| /wp-json/acf/v3/users/{id}/{field-name} | ✅ | ✅ |
| /wp-json/acf/v3/{taxonomy} 🆕 | ✅ | ❌ |
| /wp-json/acf/v3/{taxonomy}/{id} 🆕 | ✅ | ✅ |
| /wp-json/acf/v3/{taxonomy}/{id}/{field-name} 🆕 | ✅ | ✅ |
| /wp-json/acf/v3/comments 🆕 | ✅ | ❌ |
| /wp-json/acf/v3/comments/{id} | ✅ | ✅ |
| /wp-json/acf/v3/comments/{id}/{field-name} | ✅ | ✅ |
| /wp-json/acf/v3/media 🆕 | ✅ | ❌ |
| /wp-json/acf/v3/media/{id} | ✅ | ✅ |
| /wp-json/acf/v3/media/{id}/{field-name} | ✅ | ✅ |
| /wp-json/acf/v3/{post-type} 🆕 | ✅ | ❌ |
| /wp-json/acf/v3/{post-type}/{id} 🆕 | ✅ | ✅ |
| /wp-json/acf/v3/{post-type}/{id}/{field-name} 🆕 | ✅ | ✅ |
| /wp-json/acf/v3/options/{id} 🆕 | ✅ | ✅ |
| /wp-json/acf/v3/options/{id}/{field-name} 🆕 | ✅ | ✅ |
Filters
| Filter | Argument(s) |
|---|---|
| acf/rest_api/id | mixed ( string, integer, boolean ) $id string $type 🆕 string $controller 🆕 |
| acf/rest_api/key | string $key WP_REST_Request $request string $type |
| acf/rest_api/item_permissions/get | boolean $permission WP_REST_Request $request string $type |
| acf/rest_api/item_permissions/update | boolean $permission WP_REST_Request $request string $type |
| acf/rest_api/{type}/prepare_item | mixed ( array, boolean ) $item WP_REST_Request $request |
| acf/rest_api/{type}/get_fields | mixed ( array, WP_REST_Request ) $data mixed ( WP_REST_Request, NULL ) $request |
| acf/rest_api/field_settings/show_in_rest 🆕 | boolean $show |
| acf/rest_api/field_settings/edit_in_rest 🆕 | boolean $edit |
Basic example of how to use the filters, in this case I will set a new permission to get the fields
add_filter( 'acf/rest_api/item_permissions/get', function( $permission ) { return current_user_can( 'edit_posts' ); } );
Deprecated filters
| Filter | Argument(s) |
|---|---|
| acf/rest_api/type | string $type |
| acf/rest_api/types | array $types |
| acf/rest_api/default_rest_base | boolean $default string $type |
Request API version
See below how to select the Request API Version.
- Open the plugins page;
- Click the settings link under the pluing name (
ACF to REST API); - Select your version in the
ACF to REST APIsession; - Click in the button Save changes.
The other alternative is to define the constant ACF_TO_REST_API_REQUEST_VERSION in your wp-config.php
define( 'ACF_TO_REST_API_REQUEST_VERSION', 2 );
Field Settings
In this version is possible to configure the field options via admin.
The options are enabled using the filters below, by default theses options are disabled.
// Enable the option show in rest add_filter( 'acf/rest_api/field_settings/show_in_rest', '__return_true' ); // Enable the option edit in rest add_filter( 'acf/rest_api/field_settings/edit_in_rest', '__return_true' );
Editing the fields
The fields should be sent into the key fields.
Action: http://localhost/wp-json/acf/v3/posts/1
<form action="http://localhost/wp-json/acf/v3/posts/1" method="POST"> <?php // https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ wp_nonce_field( 'wp_rest' ); ?> <label>Site: <input type="text" name="fields[site]"></label> <button type="submit">Save</button> </form>
Action: http://localhost/wp-json/wp/v2/posts/1
<form action="http://localhost/wp-json/wp/v2/posts/1" method="POST"> <?php // https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ wp_nonce_field( 'wp_rest' ); ?> <label>Title: <input type="text" name="title"></label> <h3>ACF</h3> <label>Site: <input type="text" name="fields[site]"></label> <button type="submit">Save</button> </form>
Use the filter acf/rest_api/key to change the key fields.
add_filter( 'acf/rest_api/key', function( $key, $request, $type ) { return 'acf_fields'; }, 10, 3 );
Now, the fields should be sent into the key acf_fields
<form action="http://localhost/wp-json/acf/v3/posts/1" method="POST"> <?php // https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ wp_nonce_field( 'wp_rest' ); ?> <label>Site: <input type="text" name="acf_fields[site]"></label> <button type="submit">Save</button> </form>
Examples
Sample theme to edit the ACF Fields.
https://github.com/airesvsg/acf-to-rest-api-example
To-do list 🆕
https://github.com/airesvsg/to-do-list-acf-to-rest-api
Get ACF Fields Recursively🆕
https://github.com/airesvsg/acf-to-rest-api-recursive
More details:
Cache
Enable caching for WordPress REST API and increase speed of your application.
airesvsg/acf-to-rest-api 适用场景与选型建议
airesvsg/acf-to-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 77.89k 次下载、GitHub Stars 达 1.36k, 最近一次更新时间为 2017 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「wordpress」 「fields」 「wp」 「rest-api」 「acf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 airesvsg/acf-to-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 airesvsg/acf-to-rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 airesvsg/acf-to-rest-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Register advanced custom fields with object oriented PHP
A behavior for standard fields logic in CRUD based on kartik dynagrid and detailView
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 77.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1362
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-only
- 更新时间: 2017-03-20