Exposes Advanced Custom Fields Endpoints in the WP REST API v2
https://wordpress.org/plugins/acf-to-rest-api/
- Copy the
acf-to-rest-api
folder into yourwp-content/plugins
folder - Activate the
ACF to REST API
plugin via the plugin admin page
Filter | Argument(s) |
---|---|
acf/rest_api/types | array $types |
acf/rest_api/type | string $type |
acf/rest_api/id | mixed ( string, integer, boolean ) $id |
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/default_rest_base | boolean $default 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 mixed ( WP_REST_Response, NULL ) $response mixed ( WP_Post, WP_Term, WP_User, NULL ) $object |
If you do not want edit/show the fields of posts. So, you must use the filter acf/rest_api/types
add_filter( 'acf/rest_api/types', function( $types ) {
if ( array_key_exists( 'post', $types ) ) {
unset( $types['post'] );
}
return $types;
} );
The fields should be sent into the key fields
.
Action: http://localhost/wp-json/acf/v2/post/1
<form action="http://localhost/wp-json/acf/v2/post/1" method="POST">
<?php
// http://v2.wp-api.org/guide/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
// http://v2.wp-api.org/guide/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/v2/post/1" method="POST">
<?php
// http://v2.wp-api.org/guide/authentication
wp_nonce_field( 'wp_rest' );
?>
<label>Site: <input type="text" name="acf_fields[site]"></label>
<button type="submit">Save</button>
</form>
Sample theme to edit the ACF Fields.
https://github.com/airesvsg/acf-to-rest-api-example
Enable caching for WordPress REST API and increase speed of your application.