Giter VIP home page Giter VIP logo

acf-to-rest-api's Introduction

ACF to REST API

Exposes Advanced Custom Fields Endpoints in the WP REST API v2

https://wordpress.org/plugins/acf-to-rest-api/

Installation

  1. Copy the acf-to-rest-api folder into your wp-content/plugins folder
  2. Activate the ACF to REST API plugin via the plugin admin page

Endpoints

Endpoint READABLE EDITABLE
/wp-json/acf/v2/post/{id} yes yes
/wp-json/acf/v2/post/{id}/{field-name} yes yes
/wp-json/acf/v2/page/{id} yes yes
/wp-json/acf/v2/page/{id}/{field-name} yes yes
/wp-json/acf/v2/user/{id} yes yes
/wp-json/acf/v2/user/{id}/{field-name} yes yes
/wp-json/acf/v2/term/{taxonomy}/{id} yes yes
/wp-json/acf/v2/term/{taxonomy}/{id}/{field-name} yes yes
/wp-json/acf/v2/comment/{id} yes yes
/wp-json/acf/v2/comment/{id}/{field-name} yes yes
/wp-json/acf/v2/media/{id} yes yes
/wp-json/acf/v2/media/{id}/{field-name} yes yes
/wp-json/acf/v2/{post-type}/{id} yes yes
/wp-json/acf/v2/{post-type}/{id}/{field-name} yes yes
/wp-json/acf/v2/options yes yes
/wp-json/acf/v2/options/{field-name} yes yes

Filters

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;
} );

Editing the fields

The fields should be sent into the key fields.

Field Name

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>

Example

Sample theme to edit the ACF Fields.

https://github.com/airesvsg/acf-to-rest-api-example

Cache

Enable caching for WordPress REST API and increase speed of your application.

https://github.com/airesvsg/wp-rest-api-cache

acf-to-rest-api's People

Contributors

airesvsg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.