airesvsg / acf-to-rest-api
Exposes Advanced Custom Fields Endpoints in the WordPress REST API
Fund package maintenance!
www.paypal.com/donate?hosted_button_id=P4DVDKW4ZV7GE
Installs: 68 810
Dependents: 0
Suggesters: 0
Security: 1
Stars: 1 327
Watchers: 28
Forks: 111
Open Issues: 147
Type:wordpress-plugin
Requires
- php: >=5.3.2
- composer/installers: ~1.0
This package is auto-updated.
Last update: 2024-11-10 23:30:37 UTC
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-api
folder into yourwp-content/plugins
folder - Activate the
ACF to REST API
plugin via the plugin admin page
Endpoints
Filters
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
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 API
session; - 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.