├── README.md ├── acf-to-wp-rest-api.php ├── includes ├── admin │ └── views │ │ └── html-notice-upgrade.php └── classes │ ├── class-acf-to-wp-rest-api-attachment.php │ ├── class-acf-to-wp-rest-api-base.php │ ├── class-acf-to-wp-rest-api-comment.php │ ├── class-acf-to-wp-rest-api-custom-post-type.php │ ├── class-acf-to-wp-rest-api-options.php │ ├── class-acf-to-wp-rest-api-page.php │ ├── class-acf-to-wp-rest-api-post.php │ ├── class-acf-to-wp-rest-api-term.php │ └── class-acf-to-wp-rest-api-user.php └── readme.txt /README.md: -------------------------------------------------------------------------------- 1 | This version was discontinued, please upgrade to V2 2 | ================ 3 | 4 | **New version:** https://github.com/airesvsg/acf-to-rest-api/ 5 | 6 | ACF to WP REST API 7 | ================ 8 | Puts ACF data into the WP-REST-API ( WP-API | WordPress JSON API ). 9 | Also you can customize the answer using filters. 10 | 11 | Installation 12 | ================ 13 | 1. Copy the `acf-to-wp-rest-api` folder into your `wp-content/plugins` folder 14 | 2. Activate the `ACF to WP REST API` plugin via the plugins admin page 15 | 16 | Get ACF data by ID 17 | ================ 18 | - /wp-json/acf/post/`` 19 | - /wp-json/acf/page/`` 20 | - /wp-json/acf/user/`` 21 | - /wp-json/acf/term/``/`` 22 | - /wp-json/acf/comment/`` 23 | - /wp-json/acf/attachment/`` 24 | 25 | Get Options 26 | ================ 27 | - /wp-json/acf/`options` 28 | 29 | Get Option by Field Name 30 | ================ 31 | - /wp-json/acf/options/`` 32 | 33 | Sample Answer 34 | ================ 35 | ```json 36 | { 37 | "ID" : 1, 38 | "post_title" : "Post 1", 39 | "..." 40 | "acf" : { 41 | "field1" : "value 1", 42 | "field2" : "value 2" 43 | } 44 | } 45 | ``` 46 | 47 | Filter 48 | ================ 49 | Use the filter (**acf_to_wp_rest_api_`{type}`_data**) to customize the answer. 50 | 51 | The wildcard `{type}` can be: **post**, **page**, **user**, **term**, **comment**, **attachment** or **options** 52 | 53 | #### How to use 54 | ```PHP 55 | add_filter( 'acf_to_wp_rest_api_post_data', function( $data, $object, $context ) { 56 | if ( isset( $data['type'] ) && 'my_post_type' == $data['type'] && isset( $data['acf'] ) ) { 57 | // do something 58 | } 59 | return $data; 60 | }, 10, 3 ); 61 | ``` 62 | -------------------------------------------------------------------------------- /acf-to-wp-rest-api.php: -------------------------------------------------------------------------------- 1 | 8 | 9 |
10 |

ACF to WP REST API

11 |

Please, upgrade ACF to WP REST API for your latest version.

12 |

13 | ACF to REST API 14 | View on GitHub 15 |

16 |
-------------------------------------------------------------------------------- /includes/classes/class-acf-to-wp-rest-api-attachment.php: -------------------------------------------------------------------------------- 1 | type = strtolower( str_replace( 'ACF_To_WP_REST_API_', '', get_class( $this ) ) ); 15 | if ( class_exists( 'WP_JSON_Server' ) ) { 16 | add_filter( "json_prepare_{$this->type}", array( $this, 'get_fields' ), 10, 3 ); 17 | add_action( 'wp_json_server_before_serve', array( $this, 'init_routes' ) ); 18 | } elseif ( class_exists( 'WP_REST_Response' ) ) { 19 | add_filter( "rest_prepare_{$this->type}", array( $this, 'get_fields' ), 10, 3 ); 20 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); 21 | } else { 22 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); 23 | } 24 | } 25 | 26 | public function init_routes() { 27 | if ( method_exists( $this, 'register_routes' ) ) { 28 | add_filter( 'json_endpoints', array( $this, 'register_routes' ), 10, 1 ); 29 | } 30 | } 31 | 32 | public function register_routes( $routes ) { 33 | if ( class_exists( 'WP_JSON_Server' ) ) { 34 | $routes["/acf/{$this->type}/(?P\d+)"] = array( 35 | array( array( $this, 'get_fields_by_id' ), WP_JSON_Server::READABLE ), 36 | ); 37 | } else { 38 | register_rest_route( 39 | 'acf', 40 | "/{$this->type}/(?P\d+)", 41 | array( 42 | 'methods' => 'GET', 43 | 'callback' => array( $this, 'get_fields_by_id' ), 44 | ) 45 | ); 46 | } 47 | 48 | return $routes; 49 | } 50 | 51 | protected function get_id( $object ) { 52 | $this->id = false; 53 | 54 | if ( is_numeric( $object ) ) { 55 | $this->id = $object; 56 | } elseif ( is_array( $object ) ) { 57 | if ( isset( $object['id'] ) ) { 58 | $this->id = $object['id']; 59 | } elseif ( isset( $object['ID'] ) ) { 60 | $this->id = $object['ID']; 61 | } 62 | } elseif ( is_object( $object ) ) { 63 | if ( $object instanceof WP_REST_Request ) { 64 | $this->id = $object->get_param('id'); 65 | } elseif ( isset( $object->ID ) ) { 66 | $this->id = $object->ID; 67 | } elseif ( isset( $object->comment_ID ) ) { 68 | $this->id = $object->comment_ID; 69 | } elseif ( isset( $object->term_id ) ) { 70 | $this->id = $object->term_id; 71 | } 72 | } 73 | 74 | $this->id = absint( $this->id ); 75 | 76 | return $this->id; 77 | } 78 | 79 | protected function format_id( $object ) { 80 | $this->get_id( $object ); 81 | 82 | switch ( $this->type ) { 83 | case 'comment' : 84 | $this->id = "comment_{$this->id}"; 85 | break; 86 | case 'user' : 87 | $this->id = "user_{$this->id}"; 88 | break; 89 | case 'term' : 90 | if( isset( $object->taxonomy ) && $object->taxonomy ) { 91 | $this->id = "{$object->taxonomy}_{$this->id}"; 92 | } 93 | break; 94 | case 'option' : 95 | $this->id = 'option'; 96 | break; 97 | } 98 | 99 | return $this->id; 100 | } 101 | 102 | public function get_fields( $data = NULL, $object = NULL, $context = NULL ) { 103 | $this->format_id( $object ); 104 | 105 | $fields = array(); 106 | if ( $this->id ) { 107 | $fields = get_fields( $this->id ); 108 | } 109 | 110 | if ( isset( $data->data ) ) { 111 | $data->data['acf'] = $fields; 112 | } else { 113 | if ( ! is_array( $data ) ) { 114 | $data = array(); 115 | } 116 | 117 | $data['acf'] = $fields; 118 | } 119 | 120 | return apply_filters( "acf_to_wp_rest_api_{$this->type}_data", $data, $object, $context ); 121 | } 122 | 123 | public function get_fields_by_id( $id ) { 124 | return $this->get_fields( NULL, $id ); 125 | } 126 | 127 | } 128 | } -------------------------------------------------------------------------------- /includes/classes/class-acf-to-wp-rest-api-comment.php: -------------------------------------------------------------------------------- 1 | true ) ); 15 | $post_types = get_post_types( $args ); 16 | 17 | if ( ! is_array( $post_types ) ) { 18 | $post_types = array(); 19 | } 20 | 21 | $post_types = apply_filters( 'acf_to_wp_rest_api_cutom_post_types', array_diff( $post_types, $default ) ); 22 | 23 | if ( is_array( $post_types ) && count( $post_types ) > 0 ) { 24 | foreach ( $post_types as $pt ) { 25 | add_filter( "rest_prepare_{$pt}", array( $this, 'get_fields' ), 10, 3 ); 26 | } 27 | } 28 | } 29 | 30 | } 31 | } 32 | 33 | new ACF_To_WP_REST_API_Custom_Post_Type(); -------------------------------------------------------------------------------- /includes/classes/class-acf-to-wp-rest-api-options.php: -------------------------------------------------------------------------------- 1 | type}"] = array( 13 | array( array( $this, 'get_options' ), WP_JSON_Server::READABLE ), 14 | ); 15 | 16 | $routes["/acf/{$this->type}/(?P[\w\-\_]+)"] = array( 17 | array( array( $this, 'get_options' ), WP_JSON_Server::READABLE ), 18 | ); 19 | } else { 20 | register_rest_route( 21 | 'acf', 22 | "/{$this->type}", 23 | array( 24 | 'methods' => 'GET', 25 | 'callback' => array( $this, 'get_options' ), 26 | ) 27 | ); 28 | 29 | register_rest_route( 30 | 'acf', 31 | "/{$this->type}/(?P[\w\-\_]+)", 32 | array( 33 | 'methods' => 'GET', 34 | 'callback' => array( $this, 'get_options' ), 35 | ) 36 | ); 37 | } 38 | 39 | return $routes; 40 | } 41 | 42 | public function get_options( $name = NULL ) { 43 | $data = array(); 44 | 45 | if ( $name instanceof WP_REST_Request ) { 46 | if ( isset( $name['name'] ) ) { 47 | $name = $name['name']; 48 | } else { 49 | $name = NULL; 50 | } 51 | } 52 | 53 | if ( $name ) { 54 | $option = get_field( $name, 'option' ); 55 | $data = array( $name => $option ); 56 | } else { 57 | $data = get_fields( 'option' ); 58 | } 59 | 60 | return apply_filters( "acf_to_wp_rest_api_{$this->type}_data", $data, $name ); 61 | } 62 | 63 | } 64 | } 65 | 66 | new ACF_To_WP_REST_API_Options(); -------------------------------------------------------------------------------- /includes/classes/class-acf-to-wp-rest-api-page.php: -------------------------------------------------------------------------------- 1 | type}/(?P\d+)/(?P[\w\-\_]+)"] = array( 13 | array( array( $this, 'get_fields_by_term' ), WP_JSON_Server::READABLE ) 14 | ); 15 | } else { 16 | register_rest_route( 17 | 'acf', 18 | "/{$this->type}/(?P\d+)/(?P[\w\-\_]+)", 19 | array( 20 | 'methods' => 'GET', 21 | 'callback' => array( $this, 'get_fields_by_term' ), 22 | ) 23 | ); 24 | } 25 | 26 | return $routes; 27 | } 28 | 29 | public function get_fields_by_term( $id, $tax = NULL ) { 30 | $object = new stdClass; 31 | 32 | if ( $id instanceof WP_REST_Request ) { 33 | $tax = $id['tax']; 34 | $id = $id['id']; 35 | } 36 | 37 | $object->taxonomy = sanitize_title( $tax ); 38 | $object->ID = absint( $id ); 39 | 40 | return $this->get_fields( NULL, $object ); 41 | } 42 | 43 | } 44 | } 45 | 46 | new ACF_To_WP_REST_API_Term(); -------------------------------------------------------------------------------- /includes/classes/class-acf-to-wp-rest-api-user.php: -------------------------------------------------------------------------------- 1 |