.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ACF Fields to REST API
2 |
3 |
4 |
5 |
6 |
7 | Programmatically add data from Advanced Custom Fields to the Wordpress REST API
8 |
9 | Simply install and activate the plugin. All existing and new ACF fields will be imported into the API.
10 |
11 | Any ACF fields added to posts, pages or categories will appear in the API at the usual endpoints (eg: /wp-json/wp/v2/posts).
12 |
13 | If you have custom post types or taxonomies and you have exposed them to the API using `show_in_rest => true`, any ACF fields will appear at their endpoints. For more info on exposing custom post types and taxonomies to the API [read here](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/).
14 |
15 | Compatible with ACF v5.6.0
16 |
17 |
18 | Changelog
19 |
20 | v1.0 - 21.06.2017
21 | Initial commit
22 |
23 | v1.1 - 26.07.2017
24 | Updates to namespace and function naming
25 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lewisdonovan/acf-fields-to-wp-rest-api",
3 | "type": "wordpress-plugin",
4 | "description": "Programmatically add data from Advanced Custom Fields to the Wordpress REST API.",
5 | "license": "GPL-3.0-or-later",
6 | "authors": [
7 | {
8 | "name": "Lewis Donovan",
9 | "email": "lewis.donovan@sonymusic.com"
10 | }
11 | ]
12 | }
13 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
20 |
--------------------------------------------------------------------------------
/post-types.php:
--------------------------------------------------------------------------------
1 | get_data();
19 |
20 | //Bail early if there's an error
21 | if ( $request['context'] !== 'view' || is_wp_error( $data ) ) {
22 | return $data;
23 | }
24 |
25 | //Get all fields
26 | $fields = get_fields($post->ID);
27 | //If we have fields...
28 | if ($fields){
29 | //Loop through them...
30 | foreach ($fields as $field_name => $value){
31 | //Set the meta
32 | $response_data[$field_name] = $value;
33 | }
34 | }
35 |
36 | //Commit the API result var to the API endpoint
37 | $data->set_data( $response_data );
38 | return $data;
39 | }, 10, 3);
40 | }
41 | }
42 |
43 | ?>
44 |
--------------------------------------------------------------------------------
/taxonomies.php:
--------------------------------------------------------------------------------
1 | get_data();
14 |
15 | //Bail early if there's an error
16 | if ( $request['context'] !== 'view' || is_wp_error( $data ) ) {
17 | return $data;
18 | }
19 |
20 | //Get all fields
21 | $fields = get_fields('term_'.$term->term_id);
22 | //var_dump($fields);
23 | //If we have fields...
24 | if ($fields){
25 | //Loop through them...
26 | foreach ($fields as $field_name => $value){
27 | //Set the meta
28 | $response_data[$field_name] = $value;
29 | }
30 | }
31 |
32 | //Commit the API result var to the API endpoint
33 | $data->set_data( $response_data );
34 | return $data;
35 |
36 | }, 10, 3);
37 |
38 | }
39 | }
40 |
41 | ?>
42 |
--------------------------------------------------------------------------------